Electromagnetism is one of the 4 fundamental forces of universe. All particles emit an electric field that can interact but the other particles tend to cancel out an overall electromagnetic effect. Some materials are special because the have the right structure to let their particles move/reorientate to allign with the electromagnetic fields. Some materials are so easy to reorientate that the particles inside itself have enough force to allign creating a natural magnetic.
Any system that has both an electric charge and some angular momentum generates a magnetic field. If a neutral system has a magnetic field, it has some charged components somewhere inside.
Electrons orbiting the nucleus have charge and some angular momentum due to their orbit, so they generate magnetic fields. Electrons in and of themselves have a property called spin which, while it isn’t a literal rotating object, does have angular momentum associated with it.
In many materials, these little magnetic fields cancel out. This is because electron orbitals pair up electrons with opposite spins and orbital momenta, and leftover fields end up scattered with random orientation of molecules.
In ferromagnets (permanent metallic magnets), atoms have unpaired electrons, so each atom has a net magnetic field. They are normally aligned randomly, since that is a lower energy configuration and so is what the metal naturally wants to do, but we can force them to align by applying an external field, and because of some other interactions between atoms they will stay aligned – and thus we have turned the electrons’ intrinsic spin into a permanent magnet.
There are other forms of magnetism that exist. Paramagnetism is a similar thing to ferromagnetism (objects get magnetized in the same direction as an external field), but it is weaker and not permanent, and is a result of electron orbitals rather than intrinsic spin. Diamagnetism is a slightly more indirect result of paired electron orbitals, and actually results in a (even weaker) repulsive force. Permanent magnets technically exhibit all of these, since they have unpaired electrons with both intrinsic spin and orbital angular momentum, and all the paired electrons deeper inside the atoms. We don’t notice the paramagnetism and diamagnetism because the ferromagnetism is the strongest.
There are also electromagnets, which we create by taking an entire macroscopic collection of charge and giving it some angular momentum – i.e. moving it in a circle, through a wire (or more commonly a coil, since we stack the field from each loop). These magnetic fields have the benefit that we can turn them on and off by turning the current on and off.
We can actually create a permanent electromagnetic by inducing a current in a loop of superconductor. As long as it remains superconductive, the current will flow forever. This has actually been done, by creating a magnetic field, cooling the superconductor down, and then turning the field off – as the field starts to die down electrons in the superconductor respond by starting to move in a circle, and the net effect is that whatever field was going through the superconducting loop gets pinned until the superconductor warms up.
Here is how I visualize it in a simple way.
We have two brothers, Axel and Rod, who are skipping rope.
Attraction: Imagine Axel and Rod both facing to the right and skipping their ropes. Axels rope comes down in front of Axel. Rods rope is coming up behind Rod. So one rope going down and the other up. The ropes hit and tangle pulling the brothers together.
Repulsion: Now Axel is still facing to the right but Rod is now facing left. Axels rope comes down in front of Axel. Rods rope comes down in front of Rod. The ropes bounce off each other and push the brothers apart.
Latest Answers