The Solar System formed 4.6 billion years ago from the gravitational collapse of a giant interstellar molecular cloud, which began to spin and flatten out into a circumstellar disc from which the planets were born.
Over the next 10–20 million years the primordial Earth was formed with its atmosphere and oceans derived from volcanic activity and outgassing. The resulting water vapour condensed into the oceans, augmented by water and ice from asteroids, protoplanets, and comets.
Greenhouse gases kept the oceans from freezing at a time when the Sun had only 70% of its current strength and, by 3.5 billion years ago, the Earth’s magnetic field had been established to help prevent the atmosphere from being stripped away by solar winds.
Finally, a crust formed as the molten outer layer of the Earth cooled to become solid, and its continents were established by plate tectonics, driven by the continuous loss of heat from the Earth’s core.