In these cases, the repair may be beyond what you can accomplish at the side of the road, so your best "get home" solution is to lock your bike into a usable gear, usually one in the middle of it's shifting range.
The key to this is knowing how limit screws work.
|Rear derailleur with clearly labeled high and low limit screws|
Normally, this is a bad thing, but if you're looking for a quick fix to get you home, tightening the limit screws so the chain is stuck on a particular cog is just the thing you need to do.
First, you have to make sure the shifter cable is not an issue. If the problem is a damaged or broken cable to begin with, you probably don't have to worry much, but if the problem stems from the shifter, you'll probably have to cut or disconnect the cable using your multitool.
Once you've done that, you can simply tighten down the limit screws, while turning the cranks so the chain moves, until the chain moves onto a cog near the middle of the gear range
|This may be easiest to accomplish if you turn the bike upside down and balance it on the seat and handlebars.|
If you're working on the front derailleur, and you have a triple crankset, the derailleur spring tension is set to move the chain towards the lowest cog, so you'd simply tighten the Low limit screw until you got the chain to sit in the middle chainring (if it's a double crankset, the lower gear is probably adequate to ride home anyway, but triple cranksets have a super-low "granny gear" range which can make moving at any speed difficult).
Of course, you'll have to re-set the limit screws when you're fixing the actual problem, but being able to perform this simple hack can save you a long walk home!