Electrolysis is when you dissolve an ionic compound in water and then you place two metal rods called electrodes in the solution and pass a current through it. One electrode will be positively charged and is called the anode. The other will be negatively charged and is called the cathode.
Because during ionic bonding, metals give their electrons away, they lose negative charges (the electrons) and become positively charged and will be attracted to the cathode. And because non metals gain electrons, they become negatively charged and are attracted to the anode.
The non metals give their spare electrons (the ones they got from the metals) to the electric current and the electrons flow around the current to the metals where the electrons are returned to the metals.
Basically reverse ionic bonding
compound needs to be MOLTEN or in a solution, because only then will the ions be able to move around freely and conduct their charges, thereby allowing them to be attracted to each electrode.
If halide ions are present, oxygen forms at the anode (instead of the non metal)
If the metal at the cathode is more reactive than hydrogen then hydrogen is formed
If you do electrolysis with just water, hydroxide ions form (that's OH-)