I recently had this concern when traveling through Dubai with a companion who required a visa to enter UAE. Fortunately our two airlines (Emirates and BA) had a cooperation agreement which allowed us to check us and our luggage through to our final destination.
Failing that, a lot will depend on your passport and the countries you are travelling through. The UK government for instance has a concession on flights to the U.S. where many nationalities who would normally require a transit visa are allowed to enter for up to 24 hours, provided they have the appropriate visa for the U.S., and can show proof of a departing flight within 24 hrs.
In these situations, the Airlines can help a lot and should be your first port of call. They want your business and they will often do whatever they can to secure your safe and convenient passage. But ultimately, if you need a visa for your transit location, then the only sure way is to apply in advance for a transit visa.