Watching your ship head off into the distance standing on the quay in a foreign port is a vacation experience you should avoid. Though it does happen.
Here is how to ensure you do not get into that situation, and what to do if you do:
Make 100% sure you know the all aboard time. This is the latest time you have to be back on the ship. Double check as it is often different by port.
Go on cruise line excursions, as the ship will always wait for these to return - even if delayed.
If you self-tour then before you do so check with the excursion team the best travel arrangements to get back to the ship, and build in extra time for traffic delays or incidents.
If you go on a tour with an independent provider, make sure they include a guarantee to get you back to the ship on time and will make all the arrangements and cover the cost to meet up with the ship at the next port off they do not.
Take the daily program with you, or take a photo of the contact details of the local agent details on your SmartPhone. This is who you contact if you are running late or have any issues.
Take identification and a credit card with you when off the ship. This enables you to make and pay for arrangements if you need to.
The safest way to avoid missing the ship is to stick with cruise line excursions, but do not let the fear of missing the ship get in the way of seeing a destination how you would like to.