The formation of Earth and other planets in our solar system is explained by a theory called the nebular hypothesis. This theory suggests that the Sun and the planets of our solar system formed from a giant, rotating cloud of gas and dust known as the solar nebula about 4.6 billion years ago.
The process is thought to have begun when some event, such as a supernova, caused a region of the nebula to collapse under its own gravitational force. As the cloud collapsed, it spun faster and flattened into a disk. Most of the material was pulled towards the center to form the Sun.
The remaining gas and dust in the disk began to stick together and form clumps, a process called accretion. Some of these clumps became planetesimals, bodies that were around 1 kilometer or more in diameter. These planetesimals collided and merged, growing into protoplanets.
The inner planets, including Earth, were too close to the Sun and too hot for much of the gas and ice to condense, so they are composed mainly of rock and metal. The outer planets were far enough away from the Sun that they could retain gas and ice, and thus they became gas giants.
The concept of "in situ" planet formation, on the other hand, suggests that planets formed relatively close to their current orbits, from a protoplanetary disk of gas and dust around a young star. However, many scientists believe that our solar system's planets likely moved around significantly during their formation, implying that not all planets form "in situ".
For example, Jupiter and Saturn may have migrated inward and then outward to their current positions. This is known as the "Grand Tack" hypothesis. And Uranus and Neptune likely formed closer to the Sun and then moved outward, which is the "Nice model". These movements may have been caused by gravitational interactions with the gas in the solar disk or with other forming planets.