Much like the Sun itself (and the giant molecular cloud from which it formed), the outer gas giants – Jupiter, Saturn, Uranus, and Neptune – contain large proportions of hydrogen and helium gas. The two closest of these, Jupiter and Saturn, in fact are made mostly of hydrogen and helium, while Neptune and Uranus have higher water and methane content.
When the solar system first formed, those planets which came together close to the Sun were too warm to accrete much in the form of water or methane, which tended to evaporate away as gas. Instead, these became rocky planets, made up of iron and silicates (although they are surrounded by gaseous atmospheres). However, within the wide region of space now referred to as the Asteroid Belt, between Mars and Jupiter, the energy radiated from the Sun declines enough that water and methane condense into liquid form, and then even begin to freeze.
Beyond this so-called frost line, new planets were able to accumulate not only rock but water and methane ice as well, making them much denser. They then attracted free-floating hydrogen and helium left over from the nebula out of which the Sun was born. Once this process started, those planets continued to grow until they ran out of available gas to attract, becoming many times larger than the rocky planets closer to the Sun. They thus become what we know as gas giants, made up mostly of massive atmospheres with only a small rocky core rather than being predominantly rocky with a thin atmosphere (as the inner planets are). In a gas giant, moreover, there is no clear distinction between gas “atmosphere” and solid “rock”: instead, as one descends, the atmosphere simply becomes progressively thicker and soupier until it is similar to a liquid, compressed by the immense mass of the gas above it.
Absent the rocky core, this process is essentially comparable to the process by which the stars form. Indeed, any object more than about a dozen times the mass of Jupiter is referred to as a brown dwarf, an object essentially similar in composition to a star except that it lacks the mass to begin producing hydrogen fusion in its core. If an object grows large enough, it crosses over from the brown dwarf category onto the main sequence, becoming a red dwarf star.
For this reason, it is not surprising that, like the Sun itself, Jupiter and Saturn consist mostly of hydrogen and helium. Jupiter, for example, is slightly less than 90% hydrogen, and about 10% helium; less than 1% of the planet consists of heavier metals and molecules like water and methane. In much the same way, Saturn is about 96% hydrogen, 3% helium, and 1% other metals and molecules.
Farther out, Neptune and Uranus are slightly different. Neither planet is well understood, because we have never sent probes out this far, except for the brief Voyager flybys. Because they are much farther from the Sun, they also accumulated higher levels of water and methane ice, particularly below the outer levels of the atmosphere (which remain mostly hydrogen and helium). Methane alone accounts for about 2% of both Uranus and Neptune, although the vast majority of both planets still consists of hydrogen and helium.
Even farther out, beyond the orbit of Neptune, not enough gas or dust was present to undergo similar accretions, and no further large planets formed. Instead, the largest planet-like objects out this far are essentially rock-ice mixtures or are comet-like (these, too, are not well-understood), like Pluto and Eris.