After the stable period, a giant star expands into red supergiant. (It produces all the elements up to iron during nuclear fusion). When it finally runs out of nuclei to fuse it collapses due to the gravitational force, and then explodes – an exploding star is called a supernova.
The explosion throws dust and gas back into space and so another nebula is formed. A dense core remains – called a neutron star, because it is made entirely from neutrons. If its mass is large enough it can compress further to become a black hole. (Their gravity is so strong that not even light can escape!)