The most accepted theory on how the creation of the solar system is core accretion, which took several steps over some 4.6 billion years. The collapsing of gravity caused the material in a nebula to spin. That created the Sun, with smaller particles fanning out and forming planets such as Earth. Another theory was that the Sun formed by core accretion, but the planets, including Earth, formed from the collision of larger objects that already had outer shells.
In the core accretion method, the Earth's rocky core formed first from the bonding of heavier materials. Eventually it was covered by lighter material that formed the beginning of the crust. Celestial bombardment, earthquakes, volcanoes and plate drift helped form the Earth's topography.
Meteorites and comets crashed into the Earth, adding their material to the crust and triggering volcanoes and earthquakes. Scientists aren't sure if liquid water existed during this volcanic upheaval, but at some point condensed water vapor started to build the atmosphere. That atmosphere started to cool the planet, which caused the crust to solidify. Water vapor created copious amounts of rain, which formed the oceans.
Scientists also have a theory about the creation of the Moon, called the giant impact hypothesis. Some 4.48 billion years ago, fairly early in the Earth's lifespan, a massive meteorite or planetoid roughly the size of Mars struck the Earth. The material shot into space, and since it couldn't escape the Earth's gravity, started orbiting around the planet.