All electrons act as a tiny bar magnet - but the majority of them pair up with another electron having the opposite spin, which cancels the magnetic field within the atom.
Many elements have one or more unpaired electrons in the outer shell, but as soon as these form a chemical bond (ionic or covalent), the electron is again paired with another of opposite spin, cancelling the magnetic field at the level of a pair of atoms.
Some elements have unpaired electrons on inner shells, where they are protected from external bonding. In most cases, adjacent atoms will have opposite magnetic fields, cancelling the external magnetic field.
In some elements, these magnetic fields can align with adjacent atoms, to form "magnetic domains" with a strong overall magnetic field. In most cases, the domains are small, and adjacent domains have opposite magnetic fields, cancelling the effect at the microscopic level of crystal grains.
Some hard magnetic materials are resistant to moving the magnetic domain boundaries, so if the magnet is subjected to a very strong magnetic field during manufacture, it will preserve this magnetic field in the delivered magnet.
http://en.wikipedia.org/wiki/Magnet#Types_of_permanent_magnetsEach magnetic material has a Curie Temperature, above which the thermal motion of the atoms will disorder any magnetic field.
http://en.wikipedia.org/wiki/Curie_temperature