Shortly after the big bang, protons, neutrons and electrons swarmed in 10 billion degree heat [source: NASA]. Within minutes, hydrogen and then helium, known as the lighter elements, had taken shape from these atomic building blocks in a process called nucleosynthesis. (Lithium had a cameo as well.) The heavier elements didn't appear until much later, when the lighter elements underwent fusion inside of stars and during supernovas. Over time, stars sent wave after wave of these heavier elements, including oxygen, out into space where they mixed with the lighter elements.
Of course, the formation of hydrogen and oxygen molecules and the subsequent formation of water are two different things. That's because even when hydrogen and oxygen molecules mix, they still need a spark of energy to form water. The process is a violent one, and so far nobody has found a way to safely create water on Earth.
So how did our planet come to be covered with oceans, lakes and rivers? The simple answer is we still don't know, but we have ideas. One proposal states that, nearly 4 billion years ago, millions of asteroids and comets slammed into Earth's surface. A quick glance at the moon's crater-pocked surface gives us an idea of what conditions were like. The proposal goes that these weren't normal rocks but rather the equivalent of cosmic sponges, loaded with water that was released on impact.
While astronomers have confirmed that asteroids and comets hold water, some scientists think the theory doesn't. They question whether enough collisions could have taken place to account for all the water in Earth's oceans. Also, researchers from the California Institute of Technology found that water from the comet Hale-Bopp contains much more heavy water (aka HDO, with one hydrogen atom, one deuterium atom and one oxygen atom) than Earth's oceans, meaning either the comets and asteroids that hit Earth were very different than Hale-Bopp, or Earth got its regular water (aka H20, two hydrogen atoms and one oxygen atom) some other way.
Most recently, astronomers may have revealed that the former may be true. Using observations from the Stratospheric Observatory for Infrared Astronomy (SOFIA) — a converted 747 aircraft flying at high altitude with a 2.7-meter (106-inch) infrared telescope sticking out the tail section — they found that when Comet Wirtanen made its closest approach with Earth in December 2018, it was venting very "ocean-like" water vapor into space.
Wirtanen belongs to a specific family of comets called "hyperactive comets" that vent more water vapor into space than others. The researchers deduced this by comparing the ratio of observed H2O and HDO. Earth's oceans have a very specific D/H ratio (deuterium/hydrogen ratio), and it appears that Wirtanen shares that same ratio. As observing infrared wavelengths from the ground is impossible (Earth's atmosphere blocks these wavelengths), only space telescopes and SOFIA (which flies above most of the atmosphere) can make reliable observations of comets.