Ancient people built where they could and cities grew up where trade happened to focus. A lot of very old cities were built right on the sea/ocean. Marseille is one example, Barcelona another. Both cities have adjacent hills of course.
Another issue back in the day with building right on the ocean is the danger of raiders coming in from the sea. You might want to move a bit up river for protection.
I imagine at times being right on the ocean was not practicable because of weather and the fact that sand is not a good substrate for building much more than a hut.