I will try to explain it in the simplest way that I can think of. The magnetic behavior depends on the following factors:
1)the subatomic particles present in the atom
2)atoms
3)collection of atoms
4)domains(collection of collection of atoms)
1)THE SUBATOMIC PARTICLES AND 2)ATOMS
Subatomic particles have some basic fundamental properties like mass, charge, etc. In the same way they also have a property called as intrinsic magnetic moment(which is basically the measure of their tendency to align with magnetic fields)and they could be basically thought of as some sort of tiny magnets that can align themselves to a magnetic field. We can not ask why they behave like tiny magnets because it is how the universe works just like how objects with masses attract each other. The intrinsic magnetic moment of protons is about 1000 times weaker than electrons and as a result, the nucleus of an atom does not have much effect on magnetism. The movement of electrons creates a magnetic field but the magnetic field of an atom is not due to all of the electrons of an atom. The electrons in the filled shells are found in pair and their magnetic field is aligned in such a way that it cancels each other. But the electrons are not found in pairs in approximately half-filled shells and their magnetic fields are aligned in such a way that they add up rather than cancel each other out, as a result, the magnetic field of an atom depends mostly on approximately half-filled shells(more accurately incomplete shells). Some magnetic atoms are those of the elements: Ni,Co,Fe,Cr,etc.
COLLECTION OF ATOMS
: If a material's atoms are magnetic the entire material need not have to be magnetic. When a collection of atoms is taken into consideration then the following may happen:(the probability of either of them happening depends on which takes the least energy for that particular material's atoms )
A)The atoms may align their magnetic fields with each other in such a way that they add up together to create a magnetic field. When this happen the material is called ferromagnetic material like Fe(below its curie temperature).
B)The atoms may align their magnetic fields in an alternating way in such a way that they cancel out each other's magnetic field. When this happens the material is called nonferromagnetic or antiferromagnetic like Cr. (even though chromium atoms are individually very magnetic but due to this they(collection of atoms) are very nonferromagnetic )
C)The atoms may arrange themselves in a messy way in such that some of them are aligned while others are not and some even cancel each other. So basically they are very feebly magnetic and are called paramagnetic materials.
DOMAINS(COLLECTION OF COLLECTION OF ATOMS):
The image shows a magnetised and unmagnetized piece of ferromagnetic material.
The individual blocks are the collection of atoms while the entire material consists of billions of collection of atoms each with their magnetic field pointing in different directions mostly. But when the material is kept in a sufficiently strong magnetic field then it causes the individual domains to align together into a big domain (the magnetized piece) and their individual magnetic fields add up. This is what causes magnetism.