Atoms can be deflected by magnetic fields - provided the atom is first turned into an ion. Electrically charged particles are affected by a magnetic field although electrically neutral ones aren't. The sequence is:
Stage 1: Ionisation The atom is ionised by knocking one or more electrons off to give a positive ion. This is true even for things which you would normally expect to form negative ions (chlorine, for example) or never form ions at all (argon, for example). Mass spectrometers always work with positive ions.
Stage 2: Acceleration
The ions are accelerated so that they all have the same kinetic energy.
Stage 3: Deflection
The ions are then deflected by a magnetic field according to their masses. The lighter they are, the more they are deflected. The amount of deflection also depends on the number of positive charges on the ion - in other words, on how many electrons were knocked off in the first stage. The more the ion is charged, the more it gets deflected.
Stage 4: Detection
The beam of ions passing through the machine is detected electrically.