Get AppleCare. You may balk at paying an additional $169 - $349 for an extended warranty for your Mac, but AppleCare is worth it. You get an additional two years of warranty coverage, thus ensuring your Mac gets free service for almost any hardware fault for three years after you buy it. Not only that, if your Mac has enough major faults within the warranty period, Apple may even replace it with a brand-new Mac.
If you think your Mac is dying:
Back up your data. Seriously, if you haven't already started backing things up beforehand when things were going well, do it now.
Figure out if your Mac is really dying. It can be hard to tell at first if your Mac's problems are software- or hardware-related. If your Mac won't boot up at all, get your restore discs and try to boot off of those. If you can boot off of a restore disc, run Disk Utility to figure out if your hard drive has a problem by telling Disk Utility to "Repair Disk." If Disk Utility doesn't find any problems with your hard drive, you may just have a software issue, so try re-installing OS X. If you can't even boot off of a restore disc, you almost definitely have a hardware issue -- maybe even a really bad one like a dead logic board.
Have it looked at by a pro. I've heard both good things and bad things about the "geniuses" at Apple Stores. Their biggest advantage is that if you have AppleCare, the service you get from them is free. You'll help your case out a lot if you can give the genius specific information about the problem your Mac is having: when it started, what you were doing when it started, the steps you've taken to fix it. You're likely to get a lot better service if you can say, "I tried booting off the restore disc, but even though I could hear the drive spin up, it never actually tried to load anything," versus, "It just died, I have no idea what's going on."
If your Mac is dead:
What you do from here is going to depend on whether your Mac is still under warranty or not.
If your Mac is no longer under warranty: Whether you never bothered with AppleCare, or the 3-year coverage has lapsed, you're on your own now. You can still take your Mac in to get it serviced, but all repair costs are now coming out of your pocket, and many of these repairs aren't cheap. If you're savvy enough to do the repair, you can save a lot of money by buying the parts you need from a place like iFixit and fixing it yourself -- this will nearly always be cheaper than taking your Mac in to get it serviced. The bottom line is, if your Mac's no longer covered by warranty, you're going to have to weigh the cost of getting it repaired versus the cost of just getting a new Mac. If your 2007 MacBook is dead, and the cost of repairing it is going to be more than about $600, you're probably better off spending the extra $399 to get a new MacBook instead.
If your Mac is still under warranty: You have a lot more options if your Mac still has repair coverage. The most obvious one is that you can take it in for repairs and not have to worry about paying for labor or parts -- over three years of ownership, my wife's MacBook received over $1000 worth of new parts, and the only cost to us was the $249 for her AppleCare coverage.
But what if, like my wife, you've got a Mac that's just a flat-out lemon? Let's say that, like her, you bought a MacBook in June of 2007. Since then, you've had the top case replaced twice, the logic board replaced, the MagSafe adapter replaced, the display replaced, and now, less than two months from the expiration of your AppleCare coverage, your MacBook has died again -- won't boot up off its own drive, won't boot off the restore disc, won't even boot into Target Firewire Mode. At a bare minimum, the hard drive is dead; more likely, the logic board has died. Again. What now?
If you've had that many major failures with your Mac, give AppleCare a call and see about getting them to replace your Mac. It's totally at their discretion whether they replace your Mac with a new one or simply tell you to get it repaired, but the techs I spoke to told me that if you have enough major hardware failures within a certain space of time, they'll replace the entire unit with a new one. This isn't a service you're likely to get if all that's happened to your Mac is a hard drive failure; this is something Apple will do only if your Mac has had multiple failures in multiple major components.
The worst that can happen if you ask for a replacement is that they'll say "No" and just have you send it in for repairs instead. The best outcome, the one that my wife got, is that your nearly three-year-old MacBook will be replaced with a brand-new model, fresh off the Shanghai factory floor, with a faster CPU, bigger hard drive, and more RAM -- and you can get AppleCare coverage for that new Mac, too, meaning another three years of warranty coverage. You'll need to send Apple your old Mac, so don't get carried away with the sledgehammer. Put the old Mac somewhere safe until it's time to send it back.
The ultimate repair: a whole new machine
After your Mac has been repaired/replaced:
Hopefully you had a data backup on an external hard drive before this whole mess started, because your only other options are either to pay a third-party a lot of money to recover your data, or just kiss those mp3s and family photos goodbye forever and start over from scratch.
If you did have a backup through Time Machine, good news: you can bring all of your data over to your new drive/new Mac very easily. Have that external drive hooked up to your Mac the first time you boot it up, and during the setup process you'll be asked if you want to restore from your Time Machine backup. If you do the restore, expect it to take a long time -- a 120 GB transfer from our Time Capsule to my wife's new MacBook took a couple of hours. The payoff of the Time Machine restore: almost everything from her old Mac transferred to the new one, right down to the open Finder windows and the window positions she had the last time she'd run a backup. The weeklong wait for the new Mac aside, once she sat down at her new MacBook, everything was almost exactly the same as the old one.
If you're running Snow Leopard, there's even more good news: as of OS X 10.6, Time Machine is finally smart enough to be able to recognize backups from older machines as valid for your new one, making hints like this one, a lengthy and Terminal-intensive process for getting old Time Machine backups to work on a new Mac, obsolete.
The first time you enable Time Machine on the new Mac, it should detect your old backups. You'll then be asked: "Would you like to reuse the backup [Name of backup] with this computer? The backup was created on a different computer. If you reuse this backup it can no longer be used by the original computer." Your choices will be, "Do Not Backup Now," "Create New Backup," and "Reuse Backup." If you want to continue using your old Mac's backups, "Reuse Backup" is the choice you want. If you have a small drive backing up multiple Macs, like our 500 GB Time Capsule, you may still lose some older backup sessions as the drive tries to make room for a large initial backup of the new Mac, but at least you won't lose all of your old Time Machine backups the way you used to.
Having your Mac die on you can be a stressful experience. My wife spent much of the past week stressed beyond belief, even though she knew she was getting a replacement Mac, had all her data backed up, and had another Mac (mine) she could use in the meantime. But a week of angst while waiting for a new Mac to be shipped to you is nothing compared to the stress of having no way to replace your lost data, or no way to repair or replace a Mac that's suddenly gone from "best machine ever" to "very expensive paperweight."