All stars form in nebulae, which are huge clouds of gas and dust. Though they shine for many thousands, and even millions of years, stars do not last forever. The changes that occur in a star over time and the final stage of its life depend on a star's size.
Nuclear reactions at the centre (or core) of a star provides energy which makes it shine brightly. This stage is called the 'main sequence'. The exact lifetime of a star depends very much on its size. Very massive stars use up their fuel quickly. This means they may only last a few hundred thousand years. Smaller stars use up fuel more slowly so will shine for several billion years.
Eventually, the hydrogen which powers the nuclear reactions inside a star begins to run out. The star then enters the final phases of its lifetime. All stars will expand, cool and change colour to become a red giant. What happens next depends on how massive the star is.
A smaller star, like the Sun, will gradually cool down and stop glowing. During these changes it will go through the planetary nebula phase, and white dwarf phase. After many thousands of millions of years it will stop glowing and become a black dwarf.
A massive star experiences a much more energetic and violent end. It explodes as a supernova. This scatters materials from inside the star across space. This material can collect in nebulae and form the next generation of stars. After the dust clears, a very dense neutron star is left behind. These spin rapidly and can give off streams of radiation, known as pulsars.
If the star is especially massive, when it explodes it forms a black hole.