Magnetic traps function based on the principles of magnetostatics and electromagnetism. They generate a magnetic field gradient that exerts a force on magnetic or magnetizable particles, drawing them to regions of higher magnetic field strength. This allows for the precise control and manipulation of particles at the nanoscale.