Now this is a really complex subject... first, magnetism is actually divided on a bunch of "classes", some elements are diamagnetic and will be repelled by a magnetic field, other are paramagnetic and are attracted by the field, and there are the ferromagnetic elements like iron, nickel and others, that become permanent magnetics upon being exposed to a mag. field, other "classes" also exist but NVM them.
This behaviors are defined by subatomic interactions on electron fields, and charges in the nucleus, not so subatomic interactions between the molecules and the crystals they form and this sort of stuff. If you want an explanation on these topics I'd suggest you to take 6 minutes to watch the "Magnets: how do they work?" from minute physics, it's a pretty cool short video, and this channel has a lot of other cool stuff on this stuff.