Spinning electrons create an electromagnetic field. Bonding electrons can occur alone or in pairs.
Most electrons are paired, so their fields cancel.
For an object to be magnetic, the spin axes of its lone electrons must be aligned, i.e. all parallel. Suppose a large mass of H forms a star. Then the H will burn to He. When the He is used up, then the C core will collapse and burn to form Mg, etc. This process ends upon formation of Fe.