When we ask how a magnet works we are simply asking how the object we call a magnet exerts it’s magnetic field. The answer is actually quite interesting.
In every material there are several small magnetic fields called domains. Most of the times these domains are independent of each other and face different directions. However, a strong magnetic field can arrange the domains of any ferromagnetic metal so that they align to make a larger and stronger magnetic field. This is how most magnets are made.