Stars are formed in nebulas of gas and dust via gravitational collapse (accretion).
The remaining gas and dust that is swirling around the star flattens out into a disk (because of angular momentum). Planets gradually form in this disk, again via accretion (snowball effect).