Think of it like this: the sun shoots out rays of light called photons which interact with semiconductor materials inside a solar cells.
When this electromagnetic radiation interacts with the semiconductors with enough energy, it knocks an electron from its bond.
Solar energy works when photons from the sun excite an electron. These photons knock electrons free creating electron-hole pairs – electrons on one side of the p-n junction and holes on the other.
Now positive electrons and holes are free to move around, and direct current (DC) electricity is created. The electrons then travel through an external circuit, creating alternating current (AC) electricity.
Electrons are only allowed to move in a single direction inside the silicon cells, creating an electric field.
The direct current is then transformed by a solar inverter, which converts it into AC electricity.