The planets in our Solar System are believed to have formed from the same spinning disc of dust that formed the Sun. This disc, called the solar nebula, was composed mainly of hydrogen and helium, but also had other elements in smaller proportions. The nebula had a certain amount of angular momentum orbiting the forming Sun. Particles in the spinning disc began to clump together as gravity attracted them to each other. Over a few million years many of these chunks had merged together and there were about 109 objects called planetesimals, with diameters of about 1000 m. Over time the planetesimals continued to collide and join together, attracted by gravity. These larger objects, about the size and mass of our Moon, are called protoplanets. The accumulation of material to form planets in this way is called accretion.
Planetary Formation Animation
The temperature of the early solar system explains why the inner planets are rocky and the outer ones are gaseous. As the gases coalesced to form a protosun, the temperature in the solar system rose. In the inner solar system temperatures were as high as 2000 K, while in the outer solar system it was as cool as 50 K. In the inner solar system, only substances with very high melting points would have remained solid. All the rest would have vaoprized. So the inner solar system objects are made of iron, silicon, magnesium, sulfer, aluminum, calcium and nickel. Many of these were present in compounds with oxygen. There were relatively few elements of any other kind in a solid state to form the inner planets. The inner planets are much smaller than the outer planets and because of this have relatively low gravity and were not able to attract large amounts of gas to their atmospheres. In the outer regions of the solar system where it was cooler, other elements like water and methane did not vaporize and were able to form the giant planets. These planets were more massive than the inner planets and were able to attract large amounts of hydrogen and helium, which is why they are composed mainly of hydrogen and helium, the most abundant elements in the solar system, and in the universe.
Artist's impression of the early Solar System. Image credit: William K. Hartmann