Surrounding the Earth’s solid metal core is a layer of hot, liquid metal. Due to the Earth’s rotation perhaps (I honestly don’t know), this liquid metal is flowing. Scientists believe that the electric currents produced by this flowing liquid metal is the origin of the Earth’s magnetic field. The resulting field resembles that of a bar magnet. So the earth is like a gigantic (but not so strong) “bar magnet”.
As illustrated, this “bar magnet” may look flipped to many people because its north and south poles are near the geographic south and north poles respectively. The diagram is not wrong. The imaginary “bar magnet” must be oriented this way in order for the Earth’s magnetic field to be directed northward (on Earth’s surface), and compass needles to point northward.
Notice that the Earth’s magnetic field is not horizontal at most places on Earth’s surface. If a compass is held in a vertical orientation, its needle will also reveal a dip angle (also called the magnetic inclination). At the magnetic north pole, the dip angle is 90°, with the needle pointing directly into the ground!
Notice also that the “bar magnet” is not completely aligned with the Earth’s axis of rotation (actually it is not even centred with the Earth). Currently, it is off by about 11°. This misalignment means that the Earth’s magnetic field is not northward at most places on Earth’s surface. As a result, a compass needle does not point exactly towards geographic north. This deviation (called the angle of declination) can be as much as 20° at some places.