How were planets and asteroids formed?

A solar system starts out as a star surrounded by a "proto-planetary disk", a huge flat disk of gas, dust and rock, all attracted to the stars large gravity.

Over time, large objects in the disk attract dust and rock to themselves. They get bigger and bigger and... eventually you get a planet. Objects that attracted mostly gas formed gas giants like Jupiter and Saturn, objects that attracted mostly rock formed rocky planets like Earth and Mars.

Asteroids are basically what was left over, what didn't form planets. The asteroid belt between Mars and Jupiter is actually the remains of a "proto-planet" that didn't quite stick together in the formation of the solar system. It got ripped apart by Jupiter's gravity.