But light can also be absorbed by electrons, the electromagnetic energy can be turned into motion of the electron to make it jiggle up and down. So electrons can exchange energy with the photons. If the material is very dense then the photons get continually absorbed and re-emitted, absorbed and re-emitted and their energy comes into equilibrium with the energy of the electrons (which is determined by the temperature of the material). See also properties of light. So the photons we see from dense, hot material have energy which is characteristic of the temperature of the material and NOT determined by the material itself e.g. a coal (carbon) fire glows red hot and so do the metal wires which form the heating element of an electric fire. The photon energy can be seen most easily by plotting a spectrum - the intensity of light at a given wavelength versus its wavelength. Where the electrons and photons are in equilibrium the photons have a very characteristic spectral shape called (somewhat confusingly) Blackbody Radiation. This is very very important in astrophysics because all we have is the light - we cannot go there with a thermometer! Yet this says that if we are looking at something where the photons and electrons have interacted many times and come into equilibrium then we can measure the spectrum of the light, and then know the temperature of the star.
So back to stars: Now we can see a bit more of how the energy released in the center of the Sun can get out. The very high temperatures in the center means that the electrons are moving very fast, and colliding often (so they are jiggling up and down so accelerating and decelerating) and so they emit short wavelength radiation. These photons travel only a short distance before they bump into another electron and are absorbed. The photons are emitted and absorbed many many times, in random directions. But away from the center there is less material so they can go a little bit further in that direction. So the photons diffuse outwards, into the lower temperature material further from the center. But here the photons interact many times as well, so their mean energy drops a bit. It takes about a million years for a photon to get out from the center as it interacts so many times (compare that with the 2 seconds it would take if it were just able to come straight out)! Towards the top of the Sun then convection also becomes important as a way to transport the energy. And eventually we are so far out from the center of the Sun that there is little probability that a photon with interact again - photosphere. So this is what we see! Temperature is now about 6000K as opposed to the 15 million K in the interior, and we can see convection cells as granulations - see the movie about halfway down the page on the suns surface.
So we can make a model of the Sun, split it up into lots of layers. We require that each layer is in hydrostatic equilibrium, and that the energy can be generated by nuclear fusion reactions, and moved around by radiation (and less importantly by convection and even less importantly by conduction). Then the total mass is given by the mass of all the layers, and the total luminosity is the sum of all the energy generated per second in each shell (need temperatures of over 10 million K to start nuclear reactions). And this is a star. Put in the mass and composition of the Sun (mostly Hydrogen) and get out the right luminosity, temperature and radius! This is a very very well tested theory, see more of the Interior of the Sun
But there a problem. The most direct test of nuclear fusion would be to see neutrinos - no charge so doesn't interact with electromagnetic waves, and they are not nuclear particles so they can't interact via the strong nuclear force.... only via the weak force, which means most stream out from the sun with no interactions. The sun produces LOTS of neutrinos, 1012 go through your little finger each second irrespective of whether its night or day since they go straight through the Earth as well! But there are a few things which they can interact with (e.g. cleaning fluid!) - and experiments to detect this have NOT matched up with the predictions of the model - only see about half the numbers expected! This is the solar neutrino problem. So either we don't understand fusion, or we don't understand our experiments to catch neutrinos, or the models of the inside of the sun are wrong, or our ideas of how neutrinos interact with matter are wrong. But we think we do understand fusion as the luminosity of the sun comes out matching with observations. And we've done the neutrino experiments several different ways so we think its not just that we got the experiment wrong. And we can test our models of the inside of the sun using a technique called helioseismology - the sun reverberates at particular frequencies from sound waves going through it a bit like a milkbottle will ring if you hit it - the frequencies you get from the bottle depend on whats in it - fill it with water and it sounds different when you hit it than when its empty. Another page explaining this is here. So we can use this to get some observational ideas of the structure (temperature and density) of the interior of the sun. And it matches with the models. So it MUST be that we don't understand neutrinos and how they interact with matter! So here we are at lecture 3 and we've already come up against the current limits on our understanding of physics!!
See also the compilation of popular level articles on the Solar neutrino problem.
Pages 102-105 in Kuhn