A compass is an instrument that tells us the direction. Its needle, which has a magnet in it, spins freely which-ever direction you are facing-but it will always settle down pointing to the North.
The actual cause of the magnetism has to do with the interior of the Earth. The centre of the Earth has a molten metallic iron rich core that is spinning continuously. It is believed that the metals in the core have many loosely bound electrons, and that these particle can conduct electricity, it is this mass of moving electrons which product the magnetic field, which in turn, moves the needle of a compass.