Avoid crunch, it's not good for you, blah blah, see all the other comments. But sometimes you have to.
Try the "caf-nap". Drink a strong cup of coffee and immediately go to sleep (set an alarm for 45 mins or so). It can feel like a good 3-4 hour nap if it works.
In general, I'd say just make absolutely sure that that hard deadline is hard, and you can push yourself and recover afterward. Or if it's not really such a hard deadline, push it out long enough that you can recover.
If you are feeling foggy, work on the "grunt work" parts of the project (like lining up UI, or converting data or something that doesn't take much thinking), and when you are feeling more alert, work on the hard parts and firewall them off from sleepy you.
Don't take any drugs stronger than caffeine, even if your coworkers are.
Say to yourself, if I had to ship this literally four hours from now, what would I do? How could I fake it so that it appeared done? Do that. Then do it again. Then do it again until it's sort of really done.
Write down a priority list, not of importance but of implementation time. Cut the biggest item, no matter how important.
Look for parts that can be faked and retrofit later, with hardcoded data, textfiles, pure magic bullshit, whatever. Fake them.
Forget about quality. Once you're at this point, you've already missed your chance at that. Just get something done.
Make it a challenge, make it a war story. Don't let it happen again.