All atoms have tiny magnetic fields from their orbiting electrons. In certain metals: iron, cobalt, nickel, and other metals I can't remember the names of, these microscopic magnetic fields can be arranged in one direction, making the tiny magnets act as one. This is done by either an electric field from a wire or coil of wire (known as an electromagnet), by Earth's natural magnetic field, or rubbing the piece of metal with other magnets.
inb4xrain