Electrolysis is the process by which ionic substances are decomposed (broken down) into simpler substances when an Electric current is passed through them. Electrolysis is used to extract and purify metals.
http://www.bbc.co.uk/education/guides/zk96fg8/revision
A substance with movable ions can be decomposed with electrolysis, and is known as an Electrolyte.
From here, when the metals ion are in an aqueous solution (with H+ and OH- ions), then
Where reactivity can be determined from the Reactivity series.
This is because only if the metal is less reactive than hydrogen, will it be energetically favourable for the electron to jump to the hydrogen at the cathode. Furthermore, if the cathode is made of carbon (most common case), the carbon is more reactive, so a layer of metal would first need to form to give electrons to the hydrogen.