It’s been an awful long time since my quantum mechanics class, but briefly, it comes down to the net quantity of unpaired electrons on the molecular to unit crystal scale of structure.
An atom with full valence, like a noble gas, will have multiple electrons at the same energy level, or orbital state; to exist in pairs, they spin in opposite directions. This ‘spin’, individually creates a magnetic force, as it is an electron in motion. In most cases, this spin induced magnetic force is negated, by the counter spin of the electron that completes the pair.
In magnetic alloys, there is often a high net positive number of unpaired electrons that create a magnetic force. Such an unpaired electron is termed a Bohr magneton.
There was a time when you could give me a stoichiometry, and I could calculate the theoretical number of Bohr magnetons it should support.
After you have a likely alloy, you just need to align the unpaired electrons.
In a soft magnet, you can put it in a coil of cable, dump a closet full of capacitors into the coil, and the massive mag field in the coil will yank all the spins into the axis of the coil.
In harder magnets, you have to align the bulk in powder form, compress that to lock a general orientation, then sinter. Once the crystal structure is there, you zap it, and you have a forever* magnet.
Not the cleanest treatise, but I wrote most of it while stuffing Costco pizza in my face after my first ever round of golf in Taiwan.