First, we’ll go through the fundamentals of how a magnet works. Simply said, two magnetic fields—the earth’s field and the field of the compass needle, which is a magnet—try to align with each other. From a more technical standpoint, an object with a magnetic polar moment, which all magnets, including the needle, have, will experience a torque when submerged in a magnetic vector field, such as the earth’s. Because the torque vector is equal to, the needle will tend to align with the earth’s field. The magnetic field of the earth is quite near to straight north/south.
The right-hand thumb rule asserts that if the right hand’s thumb points in the direction of current, the other curled fingers of the same hand will point in the direction of the current’s magnetic field.
As electrons flow westward, the current moves eastward as well. As a result of the right-hand thumb rule, the magnetic field above the wire points south, in the same direction as the magnetic field of the earth. As a result, the compass points south.