The Earth was formed 4.54 billion years ago mainly because the conditions were just right.
In the beginning, there was nothing. All the matter and energy of the entire universe was contained in a singularity. A singularity is a point with an extremely high temperature and infinite density. It is also what exists in the center of a black hole. This singularity eventually exploded approximately 13.8 billion years ago, in an event that we call the Big Bang. This explosion flung all the matter and energy at very high speeds. This resulted in large quantities of gas and dust being spread out everywhere at a very rapid rate.
As the explosion was so energetic, the gas and dust that was spread out became hot and full of energy. Eventually, this gas and dust started cumulating and forming larger lumps. These lumps then became so hot and dense that they started to generate their own energy, and started igniting nuclear fires. These were the beginnings of stars.
In one such area, approximately 5 billion years ago, our Sun formed in exactly the same way. However, in the beginning it was just a star surrounded by a cloud of gas and dust. As the cloud spun, it collected into a disc called a solar nebula. A supernova nearby further pushed these gases and dust together. Eventually, the dust particles started combining with each other to form rocks, asteroids, planets and moons. These asteroids, planets and moons then began to fall into orbit around the Sun. Earth fell in place as the third rock from the Sun.
At this time, the Earth was not the planet that we think of now. It was essentially a huge piece of hot rock that still had other rocks crashing into it. These rocks left large craters which today are the some of the deepest parts of the oceans. One such planetesimal (rock), approximately the size of Mars crashed into Earth at one point. This ended up ripping a part of the Earth, which separated from the planet and became what we know as the Moon. Some parts of the planetesimal that crashed into Earth were absorbed by the planet.
The planetesimal crash also had another impact. The force of the crash knocked Earth sideways causing the tilt of the axis from 0 degrees to 23.5 degrees. This resulted in the seasons. As the planet moves around the Sun, the land in the hemisphere that tilted away from the Sun experiences winter, whereas the hemisphere that tilted toward the Sun experiences summer.
However, at this time, Earth could not have sustained life as we know it. Firstly, the Earth didn’t even have a crust, mantle and core, and all the elements on the planet were all mixed together like a smoothie. Furthermore, there were no oceans or continents, or even an atmosphere. The Earth was just a hot great ball due to the radioactive decay and constant meteorite collisions.
Eventually, after a few hundred million years the temperature of Earth reached 2,000C, which is the melting point of iron. This caused the Earth's core to form. However, the Earth’s surface was still molten, and it is possible that there was an ocean of magma. Over time, the planet cooled and the core, mantle and crust were formed. This also helped trigger the plate tectonics on the surface, leading to the Earth as we know it.
About 4 billion years ago, the oceans and the atmosphere formed. Scientists believe that the comets that crashed into Earth released water and gases, which eventually helped in the formation of the oceans and the atmosphere. The atmosphere was very different from today and barely contained any oxygen. However, the conditions were stable enough to warrant the development of the first single-celled organisms approximately four billion years ago.
The algae then changed the composition of Earth's atmosphere. They ate through the carbon dioxide and water, and released oxygen into the air. Around 2.5 billion years ago, there was enough oxygen in the air to allow the development of complex living organisms and eventually leading to the Earth we know today.