A magnet is any material capable of producing a magnetic field. Since any moving electric charge generates a magnetic field, electrons are tiny magnets. However, the electrons in most materials are randomly oriented, so there is little or no net magnetic field. To put it simply, the electrons in a magnet tend to be oriented the same way. This happens naturally in many ions, atoms, and materials when they are cooled, but isn’t as common at room temperature.
Some elements (e.g., iron, cobalt, and nickel) are ferromagnetic (can be induced to become magnetized in a magnetic field) at room temperature. For these elements, the electrical potential is lowest when the magnetic moments of the valence electrons are aligned. Many other elements are diamagnetic. The unpaired atoms in diamagnetic materials generate a field that weakly repels a magnet. Some materials don’t react with magnets at all.
The atomic magnetic dipole is the source of magnetism. On the atomic level, magnetic dipoles mainly are the result of two types of movement of the electrons. There is the orbital motion of the electron around the nucleus, which produces an orbital dipole magnetic moment. The other component of the electron magnetic moment is due to the spin dipole magnetic moment. However, the movement of electrons around the nucleus isn’t really an orbit, nor is the spin dipole magnetic moment associated with actual ‘spinning’ of the electrons.
Unpaired electrons tend to contribute to a material’s ability to become magnetic since the electron magnetic moment can’t be totally canceled out when there are ‘odd’ electrons.
The protons and neutrons in the nucleus also have orbital and spin angular momentum, and magnetic moments. The nuclear magnetic moment is much weaker than the electronic magnetic moment because although the angular momentum of the different particles may be comparable, the magnetic moment is inversely proportional to mass (mass of an electron is much less than that of a proton or neutron).
The weaker nuclear magnetic moment is responsible for nuclear magnetic resonance (NMR), which is used for magnetic resonance imaging (MRI).
Original Source: https://www.thoughtco.com/how-magnets-work-3976085