An electric current can be used to separate compounds into their different elements, and also to create new products using a method called electrolysis. Electrolysis works for ionic compounds where the ions can move- that means they need to be molten or dissolved.
First example: electrolysis of molten compounds, like sodium chloride
The simplest types of electrolysis to understand are where we do electrolysis on a molten substance. For example, if we heat up sodium chloride enough, it melts. The liquid contains Na+ ions and Cl- ions.
Because of the battery, electrons move through the wires from the anode through the battery to the cathode. The anode electrode becomes positively charged, and the cathode electrode becomes negatively charged. The charges on the anode and cathode then attract the ions in the molten NaCl. The positively charged sodium cations are attracted to the negatively charged cathode, because opposite electrical charges attract. The negatively charged chloride anions are attracted to the positively charged anode for the same reason.
Once the ions reach the electrodes, they interact with electrons:
At the anode, the chloride anions lose their excess electrons, and are turned into chlorine atoms. Loss of electrons is called oxidation.
At the cathode, the sodium cations gain electrons, and are turned into sodium atoms. Gain of electrons is called reduction.
Second example: electrolysis of solutions, like copper sulfate in water
The copper sulfate example is more complicated, but it is a common experiment, so it's useful to know what happens in outline.
The setup is the same as before, but there are now four types of ion in the solution. The copper sulfate gives us Cu2+ and SO42-. The water breaks down into H+ and OH-. Like before, the positive cations are attracted to the cathode and the negative anions are attracted to the anode.
At the anode, the hydroxide anions lose their excess electrons, and the oxygen from the hydroxide becomes oxygen gas. This is oxidation.
At the cathode, the copper cations gain electrons, and are turned into copper atoms. Gain of electrons is called reduction.
Although a lot of the details of electrolysis get complicated- especially once there are more than two ions involved- the main idea is pretty easy. Melt or dissolve an ionic compound so the ions can move, then use charged electrodes to pull differently charged ions in different directions.