It comes from the Age of Sail. At the bow of the ship are two timbers protruding on either side, slightly aft of the bowsprit and figurehead, known as catheads. The bow anchors are hung from them, and on an armed vessel, they can also serve as mounts for the pursuit guns. Below all of that is an open grille of wooden bars, just enough to support a human being, known as the heads. This was the most convenient place to "hang it over the side," and relieve one's bowels and/or bladder, without fouling the vessel with waste products, and with relatively minimal danger of falling overboard.Why is a toilet referred to as the head?
The only reason why I know that is that about 35 years ago, I was asked to assemble a large number of wargame miniature sailing warships, and felt that it was a good idea to pick up a couple of books on the subject. And so The Overlook Illustrated Dictionary of Nautical Terms and The Lore of Sail both entered my library.