So, for the formation of the solar system, a hypothesis or "theory" was made and it is called the Solar Nebula theory.
So here goes,
A vast cloud of dust once existed where our solar system is now. Static electricity drove the particles to begin clumping together over a period of billions of years. The clumps kept expanding because of the increased gravitational attraction they had on neighboring particles as they grew bigger. Mercury through Mars are examples of heavier clumps, or those with larger amounts of heavy elements in their compositions, that eventually evolved into the inner, or rocky, planets. The outer, gaseous planets, which include Jupiter through Neptune, were formed from lighter groupings. The sun emerged as the biggest, heaviest mass of all.
The clumps, which were now the size of planets, used gravity to catch additional clumps, which developed into moons. For instance, Mars managed to snag a few asteroids. Jupiter photographed an amazing variety of bodies. However, it should be noted that the moon of Earth is assumed to have formed as a result of a large collision that sent some of the planet's mantle into space.
Although we consider our solar system to be developed, "clump capture" is still going on. The planets constantly sweep up junk in their courses as they move through space. Saturn is gradually consuming its renowned rings, meteor showers on Earth occur as we travel through debris fields, and so on.