Your body is full of hydrogen atoms. In every hydrogen atom there are protons; positively charged particles. When the magnet is switched on every proton aligns itself in the long axis of the magnetic field. Then a changing magnetic field is applied using a radio frequency causing the protons to align to a different axis. When the radio wave is switched off the protons realign to their previous long axis, emitting a radio frequency as they give up energy. The signal is received by a coil and this information is sent to a computer.
The computer converts the information in to images which are made up of 'slices in pictures' of the area under examination. The images can also be stacked to produce a 3 dimensional image.