The sun is a big ball of gas and plasma. Most of the gas — 91 percent — is hydrogen. It is converted into energy in the sun's core. The energy moves outward through the interior layers, into the sun's atmosphere, and is released into the solar system as heat and light.
In the sun's core, gravitational forces create tremendous pressure and temperatures. The temperature of the sun in this layer is about 27 million degrees Fahrenheit (15 million degrees Celsius). Hydrogen atoms are compressed and fuse together, creating helium. This process is called nuclear fusion. As the gases heat up, atoms break apart into charged particles, turning the gas into plasma.
The energy, mostly in the form of gamma-ray photons and neutrinos, is carried into the radiative zone. Photons can bounce around at random in this zone from somewhere between a few thousand to for about a million years before traveling to the surface, according to Sten Odenwald on NASA's Ask the Space Scientist page.
Why don't we know how long it takes for a photon to travel outward from the center of the sun? For one thing, scientists can't see into the core to track a photon from its birth. Instead, they must rely on models that follow the infamous "drunkard's walk" problem. According to this scenario, the distance a drunken person travels while making random left and right turns is their typical step size times the square root of the number of steps taken. For a randomly traveling photon in the solar center, this depends on what is used for the mean free path (or average distance travel) of radiation. These numbers range from 4,000 years to millions of years, though most solar scientists tend to rely on 170,000 years.