About 4.6 billion years ago, Earth formed into essentially what we know it as today. How it formed is still under debate, as there are currently two competing models: the core-accretion model and the disk-instability model.
According to the core-accretion model, gravity collapsed a swirling cloud of gas and dust into what we now know as the solar system. Gravity then continued to act on the solar system and cause planetary bodies to begin clumping together. After the sun ignited, it swept away hydrogen and helium from the inner planets, leaving rocky worlds like Earth and Mars behind. The cores of these planets were created by dense matter sinking to a central point, while the crusts of these planet were formed by lighter rocky materials. Meanwhile, the gas giants of the outer solar system never had their lighter gases swept away.
The relatively new disk-instability model addresses two major flaws of the core-accretion model: that giants would need to coalesce quickly to retain their massive quantities of gas and that the small, still-forming planets would spiral into the sun fairly quickly. According to the disk-instability model, the larger planets formed from loose clumps of gas and dust over the course of about 1,000 years. This would allow them to ensnare all the gas that we see today and develop a stable orbit in the necessary amount of time.
Foundations of Plate Techtonics
As Earth began to coalesce and grow in mass, it started to attract other objects from the solar system that slammed into it with tremendous force. These collisions added to Earth's mass and reworked the still-forming ball of material. They also generated a great amount of heat that, along with the decay of radioactive elements, melted rocky materials in the young planet. Eventually, around 4 billion years ago, Earth began to cool and form both an iron core and silicate mantle underneath a rocky exterior.
After Earth's initial atmosphere was swept away, the planet began to belch forth a second, more hospitable atmosphere. Volcanic activity on the young planet released steam, carbon dioxide and ammonia -- three compounds that today's organisms still use for their metabolic processes. Eventually, the stream would precipitate and fill Earth with water. Then, much of the carbon dioxide was taken in and sequestered by the early oceans. At some point, anaerobic life forms emerged and began to release oxygen as a part of their metabolic processes. Large amounts of atmospheric oxygen paved the way for more complex forms of life to emerge.