I've been there a few times. There have been days, even weeks, where I just couldn't face a keyboard, but had to anyway. In the short term, this means I need a vacation. Usually, it happens at just the time in the project where that's impossible...
Immediate
When this happens, there's no way I can concentrate well enough or long enough to fix serious defects or take on a big chunk of functionality. What I can do is make the stats look good.
Pull up the "trivial" issues (you know, the crap that sits untouched for a year or two while everyone jumps on the criticals and majors), and then print that list. See how many of them you can clear before lunch, and cross them out on the printout. Now you have tangible proof that you did something useful. See if you can do better in the afternoon. (Use a different coloured pen, so you can tell at a glance.) Feel free to cherry-pick the easy ones or the ones you fancy, it's all about making the stats look good while doing as little as possible.
The fun thing about these trivial issues is that many of them will involve little to no programming. You may even get to prat about in Windows Paint for a while :)
One day of that is normally enough to get me back on track. This works especially well for me because my boss is nuts about graphs and stats.
Short term
Can you make it to the weekend? Get away from the computer. Not just the computer, any computer. Book a night away somewhere. Do it now.
Leave the laptop behind, leave your cellphone behind. Take a good book, take a notepad if you insist on thinking about work. But no email, nothing more technical than a good pen. I find this clears the mind wonderfully, and my scribbles make for a very productive time when I get back to the office on Monday.
I don't do this nearly as often as I should.
Medium term
The trick is to book a vacation, even a small one, for right after each release date, if your project is structured that way. If not, then at regular intervals. Have something other than coding to look forward to. It can be as simple as a weekend in the next town over. That way you don't find yourself where I do sometimes, having had no break since forever and carrying 20 days' annual leave over into the following year.
Long term
There has to be an exit strategy, unless you really can see yourself doing this till you retire. And I do work with some great guys who are in their 60s, so it's not impossible. How you go about forming a proper exit strategy and following it through is best left to someone older than me.
Personally, I'd rather go up than out. Either the dreaded "management" route, or a senior programmer role. Ideally, I'd like to mentor younger programmers, because I've always enjoyed that and apparently I'm good at it. There's a good few years of code-monkeying in me yet, but I'm certainly starting to ask "what next?"
Sure, the app I'm working on might help scientists cure cancer, but when it comes down to it all, I'm shuffling ones and zeroes around all day.That lack of a tangible real-world product is the one thing that makes me question the point of my work sometimes.
What would I do instead? I don't know. But there's meant to be a new suspension bridge getting built within sight of the office soon, and sometimes I think I'd like to jack it all in, put on some boots and a hard hat, and go build a bridge. I'd love to have even a small part in something huge, something I can show the grandchildren and say, "I helped make that."