Low-mass stars turn into planetary nebulae towards the end of their red giant phase. At that point the star becomes highly unstable and starts to pulsate. The outer layers are ejected by the resulting stellar winds. Planetary nebula are relatively short-lived, and last just a few tens of thousands of years.
As the outer layers drift away from the star, the remaining core shines brightly and is very hot (100,000°C+) - the core is now a white dwarf star. The ultraviolet radiation pumped out by the white dwarf causes the ejected outer layers to glow - the planetary nebula.
Over time, the enriched material from the planetary nebula is scattered into space and will be used for future generations of stars.