There are more than one type of solar panel, but nearly all work by the same fundamental physics, and the very large majority of commercially available panels are made from silicon (Si). I will discuss this type here. When silicon absorbs sunlight of a certain wavelength (shorter than ~1100 nm), an electron is excited into a state which is delocalized. In other words, this electron is now free to move around the entire panel with very little barrier. However, in a piece of plain old Si, there is no reason for it to go one direction rather than another (and electricity is the net movement of electrons in a single direction). In fact, the electron will move around in the Si randomly, and then eventually this electron will simply return to the ground, or unexcited, state and release heat (just like any black object does in the hot sun). However, in the solar panel, there is something called a junction, where two slightly different materials meet. The two materials are actually both made of Si, but each one is doped (has a tiny percentage of other atoms mixed in) with a different element. The type of doping (the two types are called n- and p-type) of the Si determines its electrical properties. When an electron reaches this junction, it is either accelerated across it, or repelled by it, depending on which direction it is coming from (imagine water and a hill... water accelerates down the hill, but can't go up it). Now there is directionality to the system. You attach a wire to each side of this junction, and when sunlight is absorbed, the free electrons now only flow in one direction, from one side of the junction to the other, driving a current through the external circuit. Electricity is just that -- electrons having a net flow in a single direction along a wire.