Answer: The planets formed at the center of a collapsing spinning cloud of gas and dust.
The most accepted theory so far, in relation to the formation of our solar system is that it was originated by the contraction of a cloud of interstellar gas due to its own gravity.
To understand it better:
According to this theory, a cloud of gas and interstellar dust began to shrink due to its own gravity. This led to the increase in the temperature of the system, which began to rotate forming a large gas sphere in the center (the Sun) with a flat disk around it.
This is how the Sun formed from the sphere in the center and the planets formed from the disk orbiting in the same plane.