A compass uses a magnet that points north according to the Earth's natural magnetic field. In many compasses, the indicator is a magnetic needle mounted on a spindle that allows it to swing freely, while in others there may be multiple magnets and a liquid base to dampen oscillations.
Earth has two magnetic poles, one near the northernmost point on the globe and one near the southernmost point. The pull of these poles is strong enough to influence magnets everywhere on the planet. The magnet inside a compass is mounted so it may swing freely, and it naturally falls in line with the Earth's magnetic field, indicating north.
Technically, the northern magnetic pole of the Earth is the south pole of the Earth's magnetic field because it attracts the "north" end of magnets. Opposite poles of magnets attract, and the north pole of a magnet is attracted to the north magnetic pole, which is the south pole of the magnetic field.
The magnetic poles do not totally correspond with the northernmost and southernmost points on the globe, and they can wander over time, slightly changing the official direction of magnetic north. In addition, the magnetic field of the Earth has weakened and grown stronger many times in history, and on occasion, the poles have even swapped places.