Please disregard the post above mine, as it's about docking bays, not docking ports.
If you get the message that you cannot leave the ship, due to all docking ports being closed, then the ship you're currently on is in hyperspace, regular space or atmosphere, thus you cannot exit the ship.
If you want to board another ship/vehicle/space station, you can only use the "Exit" button if your ship is docked inside the entity you want to board. Otherwise you will have to use the "Enter" button to enter another entity.