They have magnetic fields, opposite magnetic poles and ferrous(sp?) metals attract I Believe electro magnets just have way more electrons due to current flow.
Coz god tells them to. My Mulla at sunday school knows the answer to everything, though it's always the above to be fair.