Stars, including the sun, derive their energy from the process of nuclear fusion taking place in their cores which causes a release of light at the surface. This process converts matter into energy and releases vast quantities of neutrinos and gamma rays. The sun has a core of about 27 million degree Fahrenheit.
The neutrinos emitted by the sun barely interact with matter and travel in straight lines at the speed of light. It takes only a few seconds for neutrinos to escape the sun, and it takes only around eight minutes for them to cross Earth's orbit. Gamma rays, however, are highly energetic and interact repeatedly with the surrounding matter inside the sun. Each gamma ray can be absorbed and re-emitted by an atom thousands of times a second. Despite the speed of these interactions, gamma rays don't follow any particular path out from the sun's core, and any individual gamma particle can spend hundreds of thousands of years passing from one atom to another before reaching the sun's photosphere. Along the way, the high-energy gamma ray is broken down into large numbers of lower-energy photons, such as X-rays, and, eventually, visible light. Once the visible light photons reach the surface layers, they are free to escape into space at the speed of light.