Earth formed around 4.6 billion years ago by collisions in a giant disc-shaped cloud of material. Gravity caused this gas and dust to slowly accrete into clumps ,which became the asteroids and small early planets called planetesimals. These objects collided with each other and gradually formed bigger planets, such as Earth and the other planets in the solar system.
At its early stages, Earth was mostly molten because of the constant collisions with other bodies. One such collision with a particularly large object is thought to be responsible for the formation of the moon. These frequent collisions led to extreme volcanism on the planet. The gases spewed by the volcanoes created the primordial atmosphere and constant rain formed the oceans. Earth's atmosphere at that time had almost no oxygen and would have been toxic to humans and other modern forms of life. The planet cooled over time and formed a solid crust, which allowed liquid water to exist on the surface.
The first life forms appeared between 3.5 and 3.8 billion years ago. Photosynthetic life appeared around 2 billion years ago, adding oxygen to the atmosphere. Complex multicellular life arose around 580 million years ago, and during the Cambrian period, it rapidly spread out into most major phyla.
The details about how Earth was formed are still being worked out by scientists. They study meteorites, the oldest rocks and planets in other solar systems to understand what happened at the earliest stages of our planet.