Magnetism at atomic /sub - atomic levels is mostly due to charged particles called electrons. Electrons have spin which give them angular momentum and thus a magnetic moment associated with it. That is the cause of Magnetism at sub atomic levels. Electrons fill up orbitals in atoms in pairs.
The mechanics of how do magnets work really breaks right down to the atomic level. When current flows in a wire a magnetic field is created around the wire. Current is simply a bunch of moving electrons, and moving electrons make a magnetic field. This is how electromagnets are made to work.