Stars produce lots of energy deep in their core from nuclear fusion. The energy moves about as radiation (photons) or along with the stellar matter as it circulates through the star. The energy in the form of photons is eventually released as light from the edge of the star. In order for us to see this light, it must first pass through all the higher layers of the star. Along the way, it can be absorbed and emitted by atoms, molecules, and electrons, or it can be scattered by these species as well.
Figure 1 shows a photon being absorbed by an atom and the opposite process, a photon being absorbed by an atom as well as a photon being scattered by an atom. The energy of the photon is equal to the energy difference in the states of the atom. Sometimes the main effect is scattering in which case the direction of the photon's flight is altered, like one ball ricocheting off another ball.
All these processes occur many times in the interior of a star. The mean free path of a photon is the average distance that a photon travels before it is absorbed or scattered. For the sun, this distance about one centimeter. This is large compared to the average separation of the particles, about one nanometer, yet it is small compared to the radius of the sun, 700,000 kilometers.
Each small step the photon takes is a straight line segment but the direction varies from step to step. When a photon has to cover a large distance compared to the step size, it has to take many steps. Since it loses track of the direction of its motion, it walks "randomly" through the star. This makes a tremendous difference in how long it takes a photon to "get out." If all the steps were pointed in the same direction, the photon moving at the speed of light would leave the star in a few seconds. In fact, because the path looks more like a drunk's walk (see figure 2), it can take a million years for the energy created from nuclear fusion in the center of the sun to reach the surface.