The port fees each passenger pays covers docking or use of an anchorage, and other port facilities. When a ship leaves is legally up to the harbor master, who has to coordinate arrivals and departures, as well as the pier workers who tie up the ships and so forth. Of course these things are normally arranged well in advance and are timed quite accurately. However a ship may not always be allowed to remain docked if a ship-sponsored tour is late. If the pier space is needed a cruise ship could be required to move to an anchorage to wait for the tour to return. Certainly not a usual occurrence, but I am sure it has happened.