A star and it’s planets form from a collapsing cloud of dust and gas called a nebula. (See ‘the formation of stars’).
As the cloud gets exceedingly compressed most of the cloud begins rotating in the same direction. This rotating cloud eventually flattens into a disk that gets thinner as it spins. A bit like when you spin pizza dough. These are called ‘protoplanetary’ disks and are the birthplace of planets.
While the disk spins, the material inside travels around the star in the same direction. Eventually, the material in the disk will start to stick together and form clumps. As these clumps orbit within the disk they collect surrounding material and begin to grow larger and larger. The bigger these conglomerates get, the more material they will attract and the bigger they will grow. Soon, baby planets or ‘planetesimals’ will take shape.
If the ‘planetesimal’ is in the outer part of the disc some gases and ices haven’t yet been vaporised by the star and are used by the planetesimal to form into giants of ice and gas.
As the planetesimals grow larger they collect more and more gas and debris in their path to clear the way. This continues until the planet has a clear lane of traffic in front of it.
After millions of years all the planetesimals have cleared out much of the debris and hve formed much larger and many fewer planets that dominate their regions. The planetary system is reaching maturity.