The sun produces energy through nuclear fusion. The sun is a large ball of hydrogen and helium undergoing constant nuclear fusion, in which atoms of hydrogen combine to form helium and release a large quantity of energy.
The enormous pressure in the sun keeps the fusion running for huge amounts of time. Because the sun is dense and massive, it produces huge gravitational pull. These gravitational forces that keep the planets in orbit.
When two hydrogen atoms fuse, their nuclei combine and they release around 0.7 percent of their mass in energy. The mass conversion to energy is hugely productive. Coupled with the enormous number of hydrogen atoms in the sun, this results in massive energy output from the sun. These fusions result in 4.26 metric tons of energy released from the sun every second.
Much of this energy remains within the sun, as the gravity of the sun captures much of the output. However, the force of the explosions caused during fusion allows some of the energy to escape the sun's gravitational pull. This energy expands in all directions; a small proportion reaches the Earth as sunlight. The rest of the energy continues the chain of fusion reactions in the sun.