Nearly 4.6 billion years ago the solar system was a solar nebula. Gravity collapsed the cloud of dust and gas and began to spin forming the Sun as in the centre of the nebula.
The solar wind swept away lighter elements, such as hydrogen and helium, from the closer regions, leaving only heavy, rocky materials to form clumps to create terrestrial worlds like Mercury, Venus, Earth, and Mars.
The solar winds had less impact on lighter elements, allowing them to form the Jovian planets (gas giants). These planets are Jupiter, Saturn, Neptune and Uranus. In this way, asteroids, comets and moons were created.
Earth's rocky core formed first, with heavy elements colliding and binding together. Dense material sank to the centre, while the lighter material created the crust. The flow of the mantle beneath the crust causes plate tectonics, the movement of the large plates of rock on the surface of the Earth. Collisions and friction gave rise to mountains and volcanoes, which began to spew gases into the atmosphere.
Earth suffered an impact by a large body that catapulted pieces of the young planet's mantle into space. Gravity caused many of these pieces to draw together and form the moon, which took up orbit around Earth.
Dorward, L. (2019, March 02). Which Planets Are the Gas Planets? Retrieved from https://sciencing.com/planets-gas-planets-8392334.html Jessa, T. (2015, December 24). Earth Formation. Retrieved from https://www.universetoday.com/58177/earth-formation/ Redd, N. T. (2016, November 01). How Was Earth Formed? Retrieved from https://www.space.com/19175-how-was-earth-formed.html