Sometimes, dogs pant for the same reason we do: they're out of breath. Other times, dogs pant when it's hot because it's their way of cooling off. You may not find panting very useful, but that's because your body gets rid of heat more effectively than a dog's—you'll notice that when you go out in the winter, only one of you needs to put on a heavy coat.
Science of Sweat
Water locks in heat and carries it away from your body. That's why we sweat when we get hot—our bodies are regulating our temperatures, forcing excess heat out in beads of sweat. Since we have unobstructed pores all over our bodies, sweating comes pretty easily. Dogs, however, don't have that luxury. The only place where a dog can sweat is his foot pads, and the rest of his body is covered in a fur coat that he can't take off. Since he can't sweat, what's a hot dog to do?
This is why dogs pant: to keep cool. When a dog opens his mouth and pants, he's releasing moisture the best way he can. This is why your dog's breath is so hot and moist—not particularly appealing, perhaps, but it's his body's most efficient way of dispensing that extra body heat. A dog's mouth breath is actually warmer than his nasal breath, so when he opens up that trap, his tongue actually expands and he pushes heat straight out of his body.
Of course, panting isn't always just how your dog stays cool. Certain medical conditions can cause your dog to pant—not necessarily because he's hot, but because he's out of breath. Heart failure, injuries and respiratory disorders like pneumonia can make him pant more than usual. If your dog isn't chronically ill, he may have exercised too vigorously, or become overheated. An overheated dog is going to pant relentlessly in an attempt to cool down, but as his doting owner, you can step in and help.
If your dog appears to be overheated and panting too much, there isn't much he can do, but you can help him cool down. For example, hold him in front of a fan or air conditioner to help his body cool down. Give him a bath in cool water, and give him cold water to drink. He may even enjoy licking an ice cube to cool off. Don't cool him down too fast, though, or it will be a shock to his system—he needs to have his temperature lowered gradually. If you suspect overheating or heatstroke, take him to a vet as soon as you can.