Towards the end of their lives, stars not massive enough to become supernovae,
expand to giant dimensions. They shed most of the hydrogen in their outer
layers as a strong stellar wind, before they contract towards a final compact
stage as white dwarves.
After this ejection process, the star remains thousands of times brighter and also much hotter than the Sun during a few thousand years. Its strong ultraviolet radiation has the effect of ionizing the previously ejected gas, which then shines before it disperses into interstellar space. The resulting nebulae (traditionally referred to as Planetary Nebulae, because of their resemblance to a planet in a small telescope) often exhibit very complex and beautiful morphologies.
For cool pictures of planetary nebulae, chech out the Hubble Space Telescope photo gallery