Ships have a set of rules to follow. Known as the COLREGS for short, you can find a summary of them here:
http://www.imo.org/about/conventions/listofconventions/pages/colreg.aspx
Rule 2 (“the prudential rule”) would be a wonderful thing for motorists to adopt. It abjures dogmatic adherence to the letter of the rules, and declares that you have a responsibility to try to avoid collision, even if it means departing from the rules somewhat. So, if there’s a collision, even if you were ‘in the right’, you could still be ‘in the wrong’.
All of the COLREGS must be thoroughly learnt in all their implications before anyone can hope to be master of any vessel, or even a watch-keeping officer. As well, there’s a host of ‘local rules’ which apply to various ports and coastal waters around the world e.g. on Sydney Harbour, vessels displaying a red diamond shape are running on fixed routes (i.e. ferries), and should not be expected to deviate/give way as the COLREGS might otherwise suggest.
As for running aground, no-one worthy of the title of master or captain would knowingly take any risk which would put their vessel in danger of grounding. Cruise liner routes are very carefully surveyed beforehand by the lines, and careful consultation made with port authorities – no-one wants to find out the hard way that the harbour is too shallow for your ship, or that there’s a bridge it won’t fit under.
In most commercial ports, a pilot will take command of the ship within the confines of the harbour. A pilot is a qualified ship’s master, who also possesses a very detailed and up-to-date familiarity with the port and the local conditions. It’s the pilot whose orders are obeyed when entering and leaving the port, not the ship’s own master/captain. And, captains don’t often argue with pilots.
Warships don’t often use pilots, but here are some ports where prudence dictates that they will make use of pilots.
At sea, merchant cargo ships usually stick very closely to prescribed routes. Those routes will have been calculated to be the fastest, and the most economical in terms of time and fuel. The saying ‘time is money’ is nowhere held in higher regard than in the shipping industry. Thus, they don’t just wander willy-nilly about the oceans.