Compass was first used in China in the 400 BC in Feng Shui (geomancy). The first Compass was a simple piece of lodestone floating on water that pointed South.
Following on from this, flat pieces of iron were used instead which, when magnetized by lodestone which is a naturally occurring magnetic ore. This was then replaced with a needle by the 6th Century.
How it works
Compass consists of a small, lightweight magnet balanced on a nearly
frictionless pivot point. The magnet is generally a needle with one end often
marked "N" for North, or colored in some way to indicate that it
points toward North.
The compass works on one fundamental principle: Earth itself is a big magnet. It is an uncanny fact that the Earth's magnetic axis lies close to its rotational axis, meaning that the magnetic poles are quite close to the geographical ones. Therefore, a compass could be used to tell direction on Earth. To use a Compass, adjust the capsule so the North on the scale is aligned with the part of the needle indicating North (usually red in color). Once it is done, turn the compass till the designated object is aligned to the needle. The reading is then read off the scale on the capsule.
A general misconception of a Compass is that its needle points to true North (geographical North). In fact, it does not, except by coincidence in some areas. The magnetic field of Earth is not uniform and varies at different latitudes of the planet. The Compass needle is attracted by magnetic force and therefore, it is fluctuating too. When the needle reads North, it is actually the direction of the magnetic North Pole. There is a slight deviation from true North and this phenomenon is called declination.
In addition, the nature of the Earth’s weak magnetic field might not be powerful enough for the Compass to point accurately in the right direction. Any wire carrying a current induces magnetic flux and since they are all around in the modern society, they affect the Compass as well.