Magnets work due to the magnetic field they produce. This magnetic field is created by the alignment of the magnetic domains within the material of the magnet. When two magnets are brought close together, their magnetic fields interact, causing either attraction (opposite poles) or repulsion (like poles). This phenomenon is based on the principles of electromagnetism and the behavior of electric charges.