Welcome to cruise-chat.
How long it takes to leave the ship will depend on if you are docked or at anchor. Docking is best since as soon as the ship is cleared by the local authorities, you can leave. This usually doesn't take long and people leave as quickly as security can process you (most cruise lines require each passenger to key card out when leaving, and you card back in on your return). Anchoring means you have to take tenders (small boats or ferries) to get ashore and this cuts into your time. Usually, guests can leave the ship within a half-hour of arrival. Guests are usually required to be back aboard 30 minutes or so before the ship sails. In other words, if a port visit is 8 hours you should figure on losing an hour of it.
Another thing to note - tendering can take a while since since the usual drill is to load the first tenders with guests who booked tours with the ship's tour desk. The first tenders to leave will also require guests who didn't book a ship's tour to get a "tender ticket" which splits up the crowd into manageable groups. It is not unusual to have up to an hour wait until your tender ticket is finally called - depending on the port. You have to be quick to get a lower numbered tickets. After a while, they will not require tickets and the tenders will come and go every few minutes.