Look at our blue planet from afar, and you could easily conclude that the Earth is nothing more than a world of water. More than 70% of its surface is covered by oceans, to an average depth of 3,700 metres. Over aeons, that water has shaped continents, built our atmosphere and contains (somewhere in its depths) the cradle of life.

To locate the source of our oceans, we should start with the raw ingredients

Today, our oceans hold millions of life forms - from bacteria to blue whales - and sit at the centre of our planet’s ecology, climate and weather. The water within drives the world’s winds, it temporarily becomes clouds or ice sheets at various locations, and it connects the poles via languorous deep-sea currents - processes that are all reflections of water’s singular role in absorbing and moving the Sun’s energy around our planet.

For these and many other reasons, as far as life is concerned, the oceans are the Earth.

But these oceans have not always existed on our planet. And the water within them is alien, arriving here many hundreds of millions of years after the Earth first took shape, 4.5 billion years ago. Back then, the surface of our planet was an unrecognisable hell - volcanic and bone dry.

Our oceans’ water, the substance precious to every life form and which has come to define our planet, arrived in frozen lumps from space during one of the most violent episodes in our planet’s early history.

Around 5 billion years ago, all the ingredients for our oceans were floating in a planetary nebula

To locate the source of our oceans, we should start with the raw ingredients. Water is the second most common molecule in the universe and each one is made from two atoms of hydrogen and one of oxygen.

The hydrogen comes from the moments after creation itself, the Big Bang. As the universe exploded into being 13.7 billion years ago, some of the energy that came out of the unimaginable fireball began to condense into particles and radiation.

Within its first three minutes, some of the newly-formed electrons and protons had slowed down enough to capture each other by mutual attraction. All of the hydrogen in the universe was made here and, to this day, it remains the most common atom in the universe.

The oxygen atoms came millions of years later. As the universe continued to expand, clouds of hydrogen clumped together and their mutual gravitational attraction eventually became so intense that the atoms at the centre of the clouds began to fuse into helium. The first stars were born and they burned for billions of years until the hydrogen fuel at their centres had run out. At that point, the stars collapsed and began to fuse their helium.

Through multiple stages of fusion, this first generation of stars produced many of the heavy elements we know - from helium to iron. Eventually the gravitational pressure within them was not strong enough to fuse the heavy atoms that had been created and the stars died in explosions that were, momentarily, brighter than the rest of the galaxies in which they existed.

Their cores collapsed into a dense collection of particles known as a white dwarf, while the explosions created vast surrounding clouds of newly-minted atoms of carbon, neon, sulphur, sodium, argon, chlorine and, crucially, oxygen.

This region of space became the factory for all our water molecules

Those stellar remnants, called planetary nebulae, are among the most beautiful objects in space. The radiation from the white dwarf star lights up the surrounding gas clouds, producing vivid fluorescent colours and astronomers have been moved to give them evocative names such as the Cat’s Eye, Starfish Twins, Blue Snowball, Eskimo and the Ant.

Around 5 billion years ago, all the ingredients for our oceans - all the hydrogen and oxygen that would end up as water molecules on the surface of our planet - were floating in the planetary nebula into which our Sun was born, igniting out of a cloud of collapsing hydrogen gas.

In that nebula, well outside the range of the young Sun’s inexorable gravitational pull that would otherwise have sucked them in, molecules and atoms floated between vastly bigger dust grains (big in atomic terms but still only a millionth the width of a human hair) made from carbon, silicon and other elements.

There wasn’t much around - just a few thousand atoms per cubic centimetre and most of that was hydrogen. But this region of space became the factory for all our water molecules.

Each water molecule on Earth started its precarious existence on one of these dust grains

The water molecules that are now in our oceans came together by chance on these carbon and silicon dust grains. The road they took to get there was achingly slow and inefficient.

On average, one hydrogen atom would land on a dust grain about once per day but, given their tiny mass, the atoms would often bounce away from the grains almost as soon as they had landed. Oxygen atoms tended to stick around for a bit longer when they hit the grains.

Randomly and very rarely, atoms of both oxygen or hydrogen would strike these grains of dust and, even more rarely, they would do so at the same time and for long enough and be close enough on the dust grain to form chemical bonds with each other.

Each water molecule on Earth started its precarious existence on one of these dust grains, when an oxygen atom and two hydrogen atoms shackled themselves to the dust and began to share their outer electrons on their new home. Over the course of hundreds of thousands of years, as it tumbled through space and collided with more hydrogen and oxygen, each grain of dust acquired successive layers of ice, until it had doubled in size. By the time the solar system was a million years old, it was full of specks of carbon and silicon carrying their mantle of irregular, amorphous ice.

Without a fully-developed atmosphere, water molecules escaped the Earth and boiled off into space

Eventually, those ice-encrusted dust grains were drawn closer together and they coalesced into slightly bigger grains. The individual particles grew, first to a few millimetres across to form tiny stones, which then combined successively into rocks, boulders, asteroids and, eventually, planets. All of the objects we know of in our Solar System appeared, phoenix-like, from the random dance of the ashes of a star that had exploded into death millions of years before.

Before the oceans could get to our planet, our planet had to form.

In its first few million years, a huge disc of rocks and ice orbited the Sun. It took 20 million years for the Earth (and other planets) to coalesce from that swirl of debris. Our early planet, 4.5 billion years ago, was a ferociously hot place. The surface was covered in volcanoes, much of the ground ran with molten magma, and huge rocks struck the surface on a regular basis.

And it rained. And rained. Possibly for millennia

One of the colliding rocks was the size of a small planet and its impact gouged out a chunk of the Earth’s crust and mantle, which began orbiting our planet and became the Moon. Underground on Earth, the decay of radioactive elements produced enormous heat. There is a reason why these first half billion years are known as the Hadean era, named for Hades, the hellish underworld of the ancient Greeks.

Most, if not all, the water on the surface of the Earth at this time came from the rocks and ice that had coalesced to form it in the first place. But the early planet had trouble keeping hold of that water. Without a fully-developed atmosphere in place, the water molecules escaped the Earth and boiled off into space.

No-one knows how many objects hit the Earth and how much water they brought

All the while, more water was being pushed to the surface by the colossal geological processes that gave Earth its internal structure. Heavy elements such as iron largely flowed to the centre, and the distinct layers of crust, mantle and core we see today began to form. Water and other volatile compounds from the rocks were driven upwards as the mantle cooled. Volcanoes and other fissures in the crust allowed superheated water vapour to escape into the atmosphere.

Around 500 million years into its life, the atmosphere and temperature had stabilised on Earth and the water vapour that had been driven into the air began to condense out. And it rained. And rained. Possibly for millennia. If nothing else, the deluge recounted by countless mythical creation stories correlates with what happened in the earliest, most tumultuous years of the Earth.

The Earth now had some water on its surface. But those early oceans, depleted by the warm conditions of the Hadean Earth, did not contain nearly the amount of water we see on our planet today.

Most of our oceans arrived from elsewhere. Around the same time as the deluge was raining down on the surface of the Earth, the inner planets of our Solar System were pummelled by comets and asteroids that were rich in alien water. The evidence for these events, known collectively as the Late Heavy Bombardment, are carved into the surface of the Moon.

Fast-forward billions of years and we are left with a planet devoid of any oceans of liquid water

No-one knows how many objects hit the Earth and how much water they brought. But this period of intense bombardment lasted from 4.5 billion to 3.8 billion years ago and, by the end of it, the Earth had all of its oceans.

Exactly where these comets and asteroids came from is uncertain. One way to work it out is to examine the relative proportions of heavy water in comets and asteroids that come from different parts of the Solar System. Heavy water contains deuterium, a form of hydrogen that contains a neutron as well as a proton in its nucleus.

Measurements from some of the most recently-studied comets - including Halley, Hyakutake and Hale-Bopp - show that they have double the proportion of deuterium in their water, compared with the water in the Earth’s oceans.

In late 2014, the mystery got deeper with early results from the European Space Agency’s Rosetta mission. Rosetta had spent 10 years flying 300 million miles through space to catch up to the comet 67P/Churyumov-Gerasimenko, one of the Jupiter family comets.

Once our oceans were in place, the next challenge faced by our young planet was to hang on to them

An on-board spectrometer found around three times more heavy water there (compared to regular water) than on Earth. If these comets are representative of the early solar system (and there is little reason to think otherwise), then they could not have provided the same water that is now on Earth – and we need to keep looking elsewhere to find the ultimate source of our planet’s water.

Once our oceans were in place, the next challenge faced by our young planet was to hang on to them. Helpfully, our planet happened to be in just the right place. The Earth formed in the Sun’s habitable zone, a distance from our star that is neither too close nor too far away for liquid water to exist on the surface. As if we needed a reminder of how lucky our location is, right next to us in the Solar System are two salutary lessons.

Venus is closer than the Earth to the Sun, and often cited as our evil twin, an example of how things might have turned out on our planet if everything had gone wrong. Its inability to hold onto oceans is a key example - the intense solar radiation on this planet would have created a humid world after the water had arrived in the Late Heavy Bombardment. Water vapour would have reached all the way to the highest reaches of the planet’s thick atmosphere.

There is evidence that water did flow on the surface of the red planet at some point in its history, but it doesn’t flow today

The higher the water went, the more likely it was to encounter energetic ultraviolet radiation coming from the Sun, whereupon each water molecule would have been torn apart into oxygen and hydrogen. The hydrogen, being so light, would easily have then escaped into space.

Fast-forward billions of years and we are left with a planet devoid of any oceans of liquid water.

Mars gives us the other extreme, showing what happens to water that ended up too far from the Sun during the Late Heavy Bombardment. When there isn’t enough solar energy to keep rivers and oceans of water moving, a planet can enter a state of runaway glaciation. The polar ice caps expand and, because water ice is white, the fields of frozen water reflect away increasing amounts of the sunlight landing on the surface.

In a vicious cycle, this causes the planet to get even colder. This is likely what happened on Mars, which orbits just outside the Sun’s liquid-water zone. There is evidence that water did flow on the surface of the red planet at some point in its history, but it doesn’t flow today.

Fortunately for us, the Earth faced neither runaway glaciation nor did its water inexorably boil away. A billion years into its life, it finally had all the pieces in place - a stable atmosphere, perfect position in the solar system and a clement environment - to maintain the vast, defining oceans that we see today.

Alok Jha is science correspondent at ITV News and the author of The Water Book, which tells the extraordinary story of our most extraordinary substance.