First, the basics
Most laptops use batteries that can last for 3-5 years, or about 1000 charges. (A premium laptop's battery might last longer.) Every time you charge your battery, the total capacity of the battery is diminished. Originally it may have had a run time of 3.5 hours, but after a year it'll run out of juice at 3 hours, even on a full charge.
If your battery capacity has diminished, there are a few things you can do about it. First, you have to correctly gauge how much capacity has been lost. There are free downloads to do this job, like Battery Bar (for Windows PCs) or Coconut Battery (for Macs). These will compare your battery's current maximum capacity to how long it lasted when it was new.
(UPDATE- we originally recommended Battery Eater and while the program works great, their download site has been compromised and we are recommending an alternate program, Battery Bar downloadable from CNET.)
Calibrating your Battery
You can't miraculously reconstitute your battery's capacity. It loses power over time due to chemical reactions taking place in the battery, as it chugs along powering your laptop. You can't undo those changes, but there is one common battery issue you can fix: In many laptops, the operating system's battery meter gets out of sync with how much juice the battery actually has.
Imagine if the gas gauge on your car dashboard was misreading how much gas you actually had in the tank. You'd either run out of gas when you thought you had a quarter of a tank left, or you'd be filling up too frequently. In your laptop, this can mean your laptop shuts down abruptly when the meter says you have 30 minutes left. Or else the meter might warn that you only have 2 minutes of battery life left and shut your laptop down, when it really has another 20 minutes remaining.
Recalibrating gets the battery meter to correctly read the current state of the battery, so you and the operating system know where you stand with existing battery life.
How to recalibrate
First, charge your laptop's battery to full, and leave it that way for at least two hours. Then unplug your laptop, and set its power management settings to never turn off or lower the monitor brightness. (HP has instructions for how do to this on Windows 7 and Vista, as well as Windows XP, while Apple has instructions for Mac laptops on their site.)
You want to drain the battery completely, then let your laptop sit for at least five hours this way -- like, say, overnight. (Just be careful and mute the volume, since some laptops make a warning sound when they're about to run out.) Afterwards, charge it up again, and you should notice a more accurate portrayal of your battery capacity. In some cases, you may even get more life out of it.
Best practices to maintain battery life
You'd think that the best way to keep your laptop's battery from wearing out is to not use it. Right?
As it turns out, batteries are like muscles; they need to be worked out regularly to stay healthy. Ideally, you'd use your laptop unplugged at least once a day, like on a train or bus commute or on the couch in front of the TV. If you're not going to use it, constantly charging your battery is a bad idea; HP recommends on their website that if you're going to leave your laptop plugged in or put up in storage for more than two weeks, you should take the battery out of your laptop.
Past the expiration date
So when is it time to throw out that old battery? The answer, surprisingly, is "never." Laptop batteries contain lots of toxic chemicals, and should never end up in landfills. Fortunately, e-stewards.org has a list of environmentally responsible recyclers that will take your old battery with no fuss.When is it time to replace your battery, then? Use the free utility apps Becky mentioned, and when they say that your battery can only hold around 25% of its original capacity it's probably time for a new one. You can buy a replacement battery from the original laptop manufacturer, and there are plenty of places online that sell discounted PC laptop batteries, like Laptops for Less and Batteries.com. Owners of newer Mac laptops can get their laptop's non-removable battery swapped out at any Apple store, with a scheduled appointment.