Most of the elements in the periodic table respond only slightly to magnetic fields. But three – iron, nickel and cobalt – are ferromagnetic.They react strongly to magnetic fields and can retain their own field if they are exposed to one. The reason is that the atoms of these metals have electrons which orbit their atomic nucleus in a slightly asymmetrical pattern. With most elements, the electrons are arranged symmetrically about the nucleus. A magnetic field has little effect on them because the force it exerts on the electrons gets cancelled out. With these three, there is a residual effect that can be permanent.
It seems that the Chinese were first to realize that the magnetic properties of iron might be useful as a direction indicator, probably in the eleventh century. The compass either reached Europe, or was independently discovered there, in the twelfth century. By this time both European and Chinese vessels were using it for navigation. It was the English scientist William Gilberd (1544-1603) who showed that for the compass to work, the Earth itself must be a giant magnet. There are, however, plenty of factors that complicate using a compass to find your way. One is that the needle will react to any magnetic influence. On ships, this means putting an elaborate, custom-built metal structure around the compass, called a binnacle, to cancel out the vessel's own magnetism. On land, the compass will alter the direction in which it points in response to bodies of iron-rich minerals inside the Earth.
And the Earth's magnetic field can change faster than one might think, turning the compass from helper to enemy. As Alan Gurney points out in his excellent book Compass, as long ago as 1701 Hailey published “An Advertisement Necessary to be Observed in the Navigation Up and Down the Channel of
England” to alert sailors to swings in the compass that risked sending them onto the rocks.