How exactly does the electrolysis of water work?
Electrolysis of pure water is very difficult, but adding only a small amount of ions make the process easily achieved. In most places, there are enough minerals in the water  that the ionic strength or conductivity of the water is  great enough for electrolysis to effectively occur without needing to add additional ions to the water 

Water dissociates into H+ ions and $\ce{OH–}$ ions; the $\ce{H+}$ ions are attracted to the negative electrode (the cathode) and are converted (reduced) to a hydrogen atom ($\ce{H}$) (i.e. $\ce{e- + H+ -&gt; H}$). This is highly unstable and immediately reacts with another hydrogen atom to produce $\ce{H2}$, molecular hydrogen gas.
At the other electrode (the anode), oxidation occurs.  The $\ce{OH-}$ ions are attracted to the positive electrode where they are oxidized to form oxygen gas ($\ce{O2}$) and hydrogen ions ($\ce{H+}$).  However, if chloride is present, it will oxidize (instead of the $\ce{OH–}$ ions) and form chlorine gas, which will then react with the water to form hypochlorous acid.

The cathode reaction is: $\ce{2H+ + 2e- (cathode) →H2 (g)}$
The  anode  reaction  is:  $\ce{2OH– → 4e-(anode) +O2 + 2H+}$
The overall reaction is: $\ce{2H2O → 2H2 (g) + O2 (g)}$