Let’s look at how solar panels work, without getting too technical. We’ll look at a silicon cells, because these are the most common, and are fairly easy to understand. Silicon cells are primarily made up of, you guessed it, silicon. Because of the atomic properties of silicon it has 4 free (valance) electrons. Each of these 4 free electrons forms a bond with an adjacent silicon atom, so it’s like each one is holding hands with 4 neighbors. This forms the structure of the solar cell. This by itself is fairly stable, so won’t produce electricity, so impurities are added. This is done to create a positive part of the cell and a negative part of the cell. A substance such as Phosphorous, which has 5 free electrons, is added to one part, and another substance such as Boron, which has 3 free electrons, is added to another part.
As you have probably already worked out, this won’t fit perfectly into our silicon structure. There will be extra electrons from the phosphorous atoms, and extra holes where the Boron atoms are. Because of the positive/negative charge difference the electrons will try to correct themselves, so the free electrons will move to the Boron side to fill the holes, making this side negatively charged, and the free holes will move to phosphorous side taking up the extra electrons, making this side positively charged.
So how is the sun involved? The sun emits photons of light, which contain energy. When photons with the correct amount of energy hit the solar cells they transfer this energy to the free electrons, knocking them loose. But they don’t just move around randomly because we have the positive and negative ends to the cell. The electrons move from the negatively charged Boron side to the positively charged Phosphorous side, creating a flow in one direction, called a current. The electrons flow into the conductors, usually a sheet of metal on the bottom and a grid of metal on the top (so the sun isn’t blocked out), and back into the other side of the cell.