Stars explode either due to gravitational collapse or runaway nuclear fusion. The core of a giant star can become so dense it collapses in on itself, releasing a tremendous amount of energy in one final explosion. Alternately, a white dwarf in a binary system may draw stellar material from its companion star, enough to overload it and create a runaway fusion reaction.
Supergiant stars, those with at least eight times as much mass as the sun, can undergo gravitational collapse due to the creation of heavy elements in the core of the star. Over the star's lifespan, it begins by forming hydrogen and helium, and then it progresses to heavier elements. By the time the star synthesizes iron in its core, it loses its ability to produce energy, and the core collapses in on itself. The collapse blows off the outer surface of the star, destroying anything in its immediate vicinity. The super-dense remnants of the star become a neutron star, so dense that a cubic inch of its material weighs millions of tons.
In some cases, a star can seem to explode without destroying itself. A star in a binary system can draw in gases and matter, increasing its density to the point where it triggers an enormous thermonuclear explosion. As long as its mass remains below a certain threshold, however, the reaction is self-limiting and the star survives.