Well, the short answer to your question is that they are far from the sun.
According to the core-accretion model, in the primordial disk of material that eventually became the planets, gasses and lighter materials farther from the sun froze and stayed available for planet-building, while lighter materials nearer the center of the disk boiled off into space. This allowed the planets that were forming farther out to accumulate large amounts of materials quickly, thus increasing in gravity. The outer planets (more than 5 Astronomical Units from the center) then were able, due to this large gravity, to pull in large amounts of gases from nearby space.
Once these planets were formed, drag and changes in angular momentum from passing other bodies nearby is believed by many astronomers to have moved them even farther away from the sun. The distance from the sun allowed the gases to cool, preventing the more volatile ones, such as Hydrogen and Helium, from boiling off into space due to heat from the sun.
Because of the immense gravity, much of the material of the gas giants is believed to exist in the form of supercritical fluids rather than actual gases.