This question already has an answer here:
It's a standard textbook answer that currents of molten iron inside the Earth create the Earth's magnetic field.
Matter, on the supermolecular scale, is electrically neutral. So any magnetic field created by the flow of electrons should be cancelled by an equal and opposite field created by the flow of equal numbers of protons in the same direction.
Apparently, due to ionisation effects, the Earth has an imbalance of electric charge but it's on the order of one Coulomb, so the residual current is on the order of microamps at the surface which is tiny anyway and furthermore would be mostly cancelled by a linearly-slower movement of equal and opposite charges further within the Earth. All together this doesn't seem like it could possibly create a magnet strong enough to deflect solar wind.
I've heard of the geomagnetic dynamo theory, but as I understand it this should suffer cancellation effects too.
How can any theory of geomagnetism work if the Earth is electrically neutral?