You have to think on the fly - often one trick can inadvertently morph into another. Sometimes I'll screw up a Biddle count trick and send the wrong card face-up into a face-down pack. I just change the patter - "Oh, look, it sent its friend away instead!"
Being sharp, however, can sometimes save a trick that might normally have gone to hell. I did a trick for a woman, "Do As I Do", and she fiddled with the deck cut - but because I watched her closely, I noticed there was only one extra card below the key card, so I realized that when I looked for her card, I needed to count an extra card past the key card. It worked perfectly and made me look like a real miracle worker, 'cause she could talk about she messed with the deck and I was still able to draw the same card she did...
But, when something goes all to hell - it goes all to hell. You have just revealed to your audience that you're human. I've screwed up tricks many times, but guess what - the audience was still entertained, because I really got into the patter. I've actually seen people spot a portion of how a trick was done, and they still found it entertaining because I wasn't simply a magician - I was a performer, an entertainer, and that part of it can't go wrong unless you let it go wrong.
The worst I've run into are people trying to mess with you by intentionally saying you got the wrong card. I switched during a trick from a standard deck to a stripper and made a point of occasionally pausing the trick and cutting straight to their card, showing them that I had their number and there was no way they could get that past me. "Are you sure you remember which card is yours? 'Cause I'd hate for you to forget in the middle of the trick, it really spoils the ending..." I don't even have to look at it; they know I can produce it any time I want. Then, when performing WOW, I showed them the WOW card, which isn't their card - it's not supposed to be - and they said, "Yes, that's my card!" At that point, I wrapped things up and left - no point in entertaining the unentertainable. It's like teaching a rock to burp after drinking a soda...