It really is a mixed bag and what is required or happens is dependent on a few variables, like your nationality, last port of call etc. You really do need to find out what is required by the country you intend to visit.
Some countries require you to get a visa in advance and also to give them notice in advance of your arrival. Others give you a stamp/visa on arrival for a set preriod ie 30 or 90 days, some are free and others charge, again depending on your nationality.
Some take your passport and give you a shorepass, these may be valid for one day or many, you get your passport back on departure.
Some countries just take your personal details(crew list) and ask that you come back and see them before you leave. Depending on your nationality and your last port of call, some say "no need to see them "and tell you to leave their office and have a nice visit.
Some countries will fine you if you are anchored in their countries waters without the proper paperwork. You must remain aboard and the officiasl visit you in some, while in others you must go ashore and find the officials to clear in.
I am sure there are even more different ways some countries handle visiting sailors.
So as you can see there is no 'one rule suits all', so do your home work before you go.
Also what a countries requirements are and what really happens can be quite different.