All the energy we see coming from the Sun was originally released in its core by the process of nuclear fusion. From there, energetic photons carry energy up through the radiation zone, bumping into protons, neutrons and electrons as they go. The centre of the Sun is the hottest and moving away from the centre it gets cooler.
At about 200,000 km beneath the solar surface it is cool enough for some of the protons, neutrons and electrons in the plasma to ‘get back together’ to form ions. Since they are no longer being bashed apart by collisions with other particles, these ions are able to absorb photons coming from the radiation zone.
They stop the radiation carrying energy any further, and the energy is then carried up to the surface of the Sun by convection.
How are temperature and density related?
At low temperatures the particles don't move around much and so stay close together (the gas or liquid is dense), but as the temperature rises, they jump around and take up more room - the material becomes less dense.