Q:

How do you deal with devastating bad beats?

Let me give you an interesting example:
You have AA, your opponent has 77.
The flop is:
A 2 7
You are happy and try to build the pot. Your opponent is happy too, but just calls.
The turn is another 7. You just did a FULL HOUSE. Awesome you think, if we both can go all-in now I am going to be very rich soon.
You bet, your opponent raises and you go all-in with a celebration smile in your face, just to find out that you lost all your money.
OBS: And no, another A did not come in the river.

A:

Really understand the maths*

It's one thing to know that one out with two cards to come is around 4% to win, it's totally another to get your head around what that really means. Most people see that they're 96% to win and completely fail to consider that they can lose from this point. You need to be able to accept that losing is a perfectly valid outcome. 4% really isn't that small a chance of that one outer coming. In a day of poker on the larger online sites there will be thousands of one-out winners. It is normal. 
You need to be able to accept that being drawn out on will happen, and not only that, it's supposed to happen. 

Play at stakes you're comfortable with.

I have to tell a bad beat story for emphasis on this one. I hadn't planned to do that but I can't think of a better way to get the point across. The biggest pot I've ever played was a bad beat. I spotted a really soft table at double my normal stakes, and slowly worked up my stack to around 125bb.
At this point I flopped top set against an underpair, villain shoved on the flop and promptly went runner runner to complete a flush with one of his cards. Unlikely, but still perfectly normal. These things happen. 
I couldn't play for three days getting over it. At my normal stakes I probably wouldn't have even quit for the day. What this makes me realise is that I was playing at stakes where the money was no longer comfortable for me. I went back to my normal stakes and haven't looked higher since.
Summary: Bad beats will affect you more when you're not comfortable with the money in play. 

Remember that you win some too.

The hand you describe is a perfectly normal cooler. Villain doesn't play bad. Set over set always gets someone stacked. If you had 77 and he had AA then the hand would have played out identically and you would have spiked the 'miracle' card. And it would have been pretty normal. 
Everybody sucks out sometimes. Good players do it less because they're less likely to get the money in behind, but even the best flop hands that are too good to get away from sometimes.

Understand yourself.

'Bad beats' sometimes come in bunches. That's normal too, but even the most controlled of players can tilt if they take a few two-outers in a row or something. You need to understand when you're off your game.
You absolutely need to be able to deal with taking a bad beat and should be able to get on with the game, but you also need to understand how much you can take before you are no longer capable of playing your 'A' game. At that point you need to be ready to step away from the game for a time to get yourself back under control.
*Disclaimer - I'm way out of practice, forgive me if I don't remember my maths correctly.

