There are multiple types of solar panel, but almost all of them work using the same fundamental physics, and almost all of the commercially available panels are made out of silicon. When silicon gets sunlight with a certain wavelength (less than ~1100 nm), an electron is excited into a state called delocalized. This electron can now move around the complete panel with very few barriers. However, in a piece of plain old silicon, there isn’t any reason for it to go in a certain direction (and electricity is the net movement of electrons in just one direction). The electron will just move around in the silicon randomly, and eventually, this electron will just go back to the ground, or unexcited, state and release heat (just like black objects do in the hot sun). However, in solar panels, there is this thing called a junction, where two slightly different materials meet. The two materials are both made of silicon, but each one is doped (has a very small percentage of other atoms mixed in) with a different element. The two types of doping (n- and p-type) of the silicon determine its electrical properties. When an electron gets to this junction, it’s either accelerated across it, or repelled by it, depending on the direction it’s coming from (Think of water and a hill…water goes down the hill fast, but there’s no way to get it to flow up). There is directionality to the system. You can attach a wire to each side of this, and when you get sunlight, the free electrons will only flow in one direction, from one side of the junction to the other, creating a current through the external circuit. Electricity is just this — electrons going in a single direction along a wire.
It needs to also be noted that commercially available solar panels only generate electricity. This is great for many things (lights, microwave ovens, air conditioners, etc.) but a lot of appliances in our homes might run on other fuels, like natural gas or oil (like hot water heaters, stoves, furnaces, etc.). If you do install solar panels on your home, you need to consider a few things. First, figure out how much energy in your house is electric and how much of it comes from fuels, like oil. Also remember that solar panels themselves don’t have any capacity to store electricity, so when the sun goes down, they don’t do anything. If you want to use electricity from your solar panels during the night, you need to also have a battery storage system and charge it up during the day and then discharge at night. Installing this is another matter completely for another article. But solar panels alone do a great job at providing day-time electricity.