Magnetism is a fundamental interaction in nature which affects moving electric charges. A moving electric charge in a magnetic field will be deflected at a right angle to the field direction. Moving electric charges in uniform magnetic fields traverse helical paths. Magnetic fields themselves are generated by moving electric charges. This is actually a relativistic effect. All electric charges create electric fields. Albert Einstein's Theory of Special Relativity says that an electric field, when viewed from a moving reference frame, will look like a magnetic field.
When many electric charges flow in the same direction, the result is an electric current. Long current-carrying wires will produce a magnetic field in the shape of concentric circles around the wire. A circularly shaped current-carrying wire will produce a field in a shape known as a dipole. Spinning spheres of electric charge will likewise produce dipole-shaped fields. Certain types of metals, including iron, nickel, and cobalt (all found abundantly on Earth), also exhibit magnetic fields, even when no current is passing through them. This is an effect of quantum mechanics. Subatomic particles such as electrons possess an intrinsic and immutable spin. Even though they have no size, they behave as spinning spheres of charge, and thus produce magnetic dipohl fields. Normally these dipoles are oriented in random directions and tend to cancel out. However, in magnetic materials all of these dipoles can be made to point in the same direction, thus resulting in a net magnetic field.
Magnetic fields were used extensively on Federation starships, most notably in the containment of antimatter. Federation-designed warp drive systems employed a magnetic containment field to contain anti-deuterium prior to its injection into the warp reactor. This field confined the anti-deuterium nuclei to circular motion, thus preventing them from annihilating with any normal matter. If a warp drive's magnetic field strength falled below a critical value, antimatter containment would be lost, resulting in the destruction of the starship. (TNG: "11001001")