How do magnets work?

How do magnets work and why are they reacting different to some sort of metals? The whole Earth has a so-called dipole moment with a south pole and a north pole. The picture shows the magnetic field lines th...