The term dates back centuries to when the toilet was placed at the front of the ship, also called the head. The toilet was situated down near the bowsprit so that sloshing waves could help clean it.
The bowsprit is connected to the main part of the hull. It is the long projection from the front part of the ship. The figurehead of the ship, if present, is usually mounted near there.
The term "head" for the front of the boat dates back at least to the 15th century. One of the first written usages of "head" to refer to a toilet is from 1708, when Woodes Rogers wrote it in his book "A Cruising Voyage Around the World."