Each atom in a piece of iron is a magnet, with a north pole and a south pole. Most pieces of iron are not magnetic, because the atomic magnets all point in different directions.
When you bring a magnet near a piece of iron, the iron-atom magnets line up with the applied magnetic field: The north poles of the iron atoms all point in the same direction. Because the iron atoms line up, the piece of iron becomes a magnet and is attracted to the original magnet.
In a rod-shaped piece of iron, the atoms will tend to line up so that all the north poles face one end of the rod and all the south poles face the other end. Since iron filings are rod-shaped, the atoms line up pointing along the length of the rod, and the rods line up parallel to the direction of the applied magnetic field. The field of a cylindrical magnet comes out of the end of the magnet and then loops around next to the side. The iron filings stick out like a crew cut on the ends of the magnet but lie flat on the sides (click to enlarge diagram below).
Because the iron filings become magnets themselves, their presence slightly changes the shape of the magnetic field. Even so, this Snack gives an indication of the shape of the magnetic field in three dimensions.
Note that if you’ve sealed the plastic bottle really well by jamming the test tube into its mouth, the sides of the bottle will begin to collapse inward after a few hours—particularly if the inside of the bottle is damp. This happens because the iron filings will begin to rust. As the iron rusts, it combines with and removes oxygen from the air trapped in the bottle. To prevent the bottle’s collapse, simply punch a small hole in the plastic with a pushpin. Black sand from the beach—which is made of magnetite and does not rust—can also be used as a substitute for iron filings.