Planets are thought to form at the same time that their star forms (see /cobaltj11-zone/2011/06/how-are-stars-made for how stars form). Stars form in clouds of dust and gas. While the star is forming, the cloud around the protostar starts to rotate around the star and will flatten into a disk around the protostar. We call this a protoplanetary disk. As it is spinning around the protostar, bits of gas and dust will start to clump together and this forms the planets.
Comments