A harbor provides cities with a trade opportunity, which is very important for the city. If they're a transfer port, they can make profit of providing services next to a harbor.
Also, a trade will provide the city with the good it itself can't produce, and a possibility to exchange the goods it can produce for the ones it needs.