The early environment around a new star, the protoplanetary disk, contains dust and gas left from the collapse of the interstellar cloud that led to the star formation. The star and the disk of debris rotate on a common center of gravity, and this disk of debris could lead to the formation of planets.
There are currently two main theories about planetary disk formation:
A post I wrote a few years ago, but a couple of things are still relevant.
I wrote this post in September 2015 when there was great excitement for the possible discovery of flowing water on the martian surface. The mentioned study was updated in 2017 and can be found here: Recurring Martian Streaks: Flowing Sand, Not Water?