The most massive stars of all end their lives in a supernova explosion. An explosion that is so powerful that one can be bright enough to outshine a galaxy! They are rare though, only two or three are expected in our galaxy every century.
When the star has used up all of its fuel, it becomes a red giant. At this point is starts fusing hydrogen outside of the core and fusing helium and heavier elements inside the core. This keeps it going for a few more million years but eventually, it can no longer hold back gravity and the core begins to collapse.
The core is like an onion, with many layers of different elements. As the core collapse material begins to bounce and this causes a shock-wave big enough to expel all of the outer material.
The material expelled remain for millions of years. Some can form new stars and even planets if there is enough mass to do so. It is believed that our own Sun and Solar System was formed out of the material of a previous super massive star. The evidence for this is all the heavy elements discovered here on Earth that can only have formed in the core of a super massive star.