Planets are the byproducts of stars. All planets in our solar system together with moons, asteroids and comets sum up to 1 percent of the solar systems mass. The other 99 percent is our Sun.
The forming of planets begins with the birth of a star. A big cloud of gas and dust begins to collaps under its own gravity and the rotation increases. The fragments turn into a protostar. A protostar glows red under the pressure of its own gravity.
When the compression is big enough the stars core ignites hydrogen burning nuclear reactions. The protostar turns into a star.
The formation of terrestrial and giant planets
It is the so-called nebular theory that planet forming is common in the universe. The planets are formed from the disk of gas and dust surrounding the infant star.
Infrared observations have revealed that most stars younger than 10 milion years old are surrounded by disks of gas and dust, called protoplanetary disks.
The small terrestrial planets are formed from fragments that compress together into planetesimals. Once planetesimals grow to a diameter of a kilometer gravity speeds up the process and the planet is being built.
Dating of meteorites suggests that planet forming is a process that takes between ten and a few hundred million years to complete.
There are two competing theories regarding to the fomation of giant planets. It's possible that a large terrestrial planet formed first, which attracted gas and dust in the early new solar system.
The other theory suggests that when the protostar is being born other layers of gas and dust collapse under their gravity and eventually form a giant planet. In this theory the gas planets are born around the same time as the (proto)star. The former theory would mean that gas giants become giants a few hundred million years later.