Superconductivity often emerges in the proximity of, or in competition with, symmetry breaking ground states such as antiferromagnetism or charge density waves (CDW)1-5. A number of materials in the cuprate family, which includes the high-transition-temperature (high-Tc) superconductors, show spin and charge density wave order5-7. Thus a fundamental question is to what extent these ordered states exist for compositions close to optimal for superconductivity. Here we use high-energy x-ray diffraction to show that a CDW develops at zero field in the normal state of superconducting YBa2Cu3O6.67 (Tc = 67 K). Below Tc, the application of a magnetic field suppresses superconductivity and enhances the CDW. Hence, the CDW and superconductivity are competing orders in this typical high-Tc superconductor, and high-Tc superconductivity can form from a pre-existing CDW state. Our results explain observations of small Fermi surface pockets8, negative Hall and Seebeck effect9,10 and the Tc plateau11 in this material when underdoped.