They have nothing to do with the Earth being closer or farther away from the Sun. That is why the Ptolemaic model of the solar system almost worked. If the orbits of the planets were very skinny ellipses, the Ptolemaic model would have made some egregiously wrong predictions. The Earth's Magnetosphere Discovered in the 1950's. Carry around a compass, you would see the needle change and move in response to the Earth's magnetic field.
Earth is like a giant bar magnet with a North pole and a South pole.
The magnetic poles don't quite match the rotational poles. Scientists think the magnetic field is created by the currents of rotating conducting material in the center of the Earth. This is called the dynamo theory , but it is still controversial. Although, the fact that the rotational and magnetic poles almost match is a good hint that we are moving in the right direction. These causes the aurora borealis in the northern hemisphere and the aurora australis in the southern hemisphere.