Stars generate energy through nuclear fusion, where lighter nuclei combine to form heavier nuclei, releasing energy in the process. In the core of stars like the Sun, hydrogen nuclei fuse to form helium. The balance between gravitational collapse and outward radiation pressure from fusion reactions determines a star's lifecycle and its eventual fate, be it a white dwarf, neutron star, or black hole.