A compass is an instrument that tells us the directions. Its needle which has a magnet in it , spins freely whichever direction you are facing but it will always settle down point ion 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 particles can conduct electricity. It is this mass of moving electrons which produces the magnetic field, which in turn, moves the needle of a compass.