Magnets stick together because they have a magnetic field around them, and this field both pulls on and pushes away other magnets.
If you've played with magnets like those that stick to your refrigerator, you know that sometimes they stick to each other, but that they also can push each other away, depending on which way the magnets are pointing.
Each magnet has a north pole and a south pole (named after the earth's magnetic north and south poles), and the north end of one magnet will stick to the south end of another magnet. But if two north poles or two south poles are placed near each other, the magnets will push each other away. That is opposite poles attracts each other and same poles repel each other.