energy is generated by nuclear fusion. it is similar to a hydrogrn bomb. when two hydrogen comes together, there is a probablity that the two hydrogen atom will fuse together to form a helium nucleus (quantum tunneling) a large amount of heat is produced, which pushes the sun boundary outwards.
the sun is at equilibrium when the nuclear force balances the gravitational force (which pushes materials inwards towards the core).
when the sun runs out of hydrogen it collapses at first until the temperature rises rapidly (gravitaional potential energy is converted to heat). the increase in temperature causes heavier elements to fuse, and the sun expands again. it is now a supernova.
at the end, when all elements are exhausted, the sun becomes a white dwaft, or a neutron star. (it usually does not have enough mass to collapse into a black hole.)