I'm about to get a visa that will outlast my passport by roughly three or four months. The time difference is not a lot and probably won't have any big effect on anything but it got me wondering. Does the expiration of a passport have any effect on still valid visas? Is this visa still usable even though the passport isn't? Or does this vary from country to country?
Many countries allow you to use a valid visa in an expired passport, as long as you present a valid passport along with it and all the other details (usually including your name) have not changed:
For example, for the United States:
My old passport has already expired. My visa to travel to the United States is still valid but in my expired passport. Do I need to apply for a new visa with my new passport?
No. If your visa is still valid you can travel to the United States with your two passports, as long as the visa is valid, not damaged, and is the appropriate type of visa required for your principal purpose of travel. (Example: tourist visa, when your principal purpose of travel is tourism). Both passports (the valid and the expired one with the visa) should be from the same country and type (Example: both Uruguayan regular passports, both official passports, etc.). When you arrive at the U.S. port-of-entry (POE, generally an airport or land border) the Customs and Border Protection Immigration Officer will check your visa in the old passport and if s/he decides to admit you into the United States they will stamp your new passport with an admission stamp along with the annotation "VIOPP" (visa in other passport). Do not try to remove the visa from your old passport and stick it into the new valid passport. If you do so, your visa will no longer be valid.
Or the UK (but see the link a bunch of details where this does and doesn't apply):
You can use the valid visa in your expired passport when you’re travelling to and from the UK. You’ll need to travel with your expired and your new passport.
This is true of most countries, but you'll need to check for official advice from a particular country to ensure their rules work the same way and whether there are any conditions.
If this is allowed by the country you're traveling to, you will also need to ensure your own country allows you to keep your old passport and that it is returned to you after renewal (some countries require that you request this specifically or it will be destroyed). See Can I keep my old passport when I have it renewed and a new one issued?
As mentioned above, it'll depend on the country, and possibly the type of visa. Many countries (most?) ask that the passport's validity covers at least the length of the visa. In all the countries I have applied a visa for (mostly Asia), both for visits and residence, I had to document the validity of my passport, and it had to be equal or longer to the length of stay. For tourists visas I was often asked to provide a passport that had a validity exceeding the length of stay by 3 to six months, which makes sense, as these were countries where you can extend your tourist visa in-country.
A notable exception I can think of is visas that by default exceed most passports' validity, like the 10-year visas given by some countries. But these aren't the most common kind of visa.
For resident visas, it could be argued that as a resident you can easily extend/renew your passport at the consulate, but the countries I have dealt with prefer to play it safe.
This happened to me with my 10-year India visa, and their policies at the time (not sure if they're still the same) required submitting both the old passport with visa, and new passport, for the visa to be "moved" (cancelled on the old passport, and a new one affixed to the new passport with the original expiration date). Of course it also carried a nice fee for the service. This definitely varies by country, and you should check the policies of the country that issued the visa.