Avoiding "hat hair" is pretty obvious. Don't wear hats. Taking a shower and washing your hair will also get rid of hat hair. Sometimes, especially in cool weather, hats are necessary. But what if you aren't near a shower? There are a few easy things you can do to get rid of that hat line.
First, avoid putting a hat on hair that is damp (it should be completely dry)and try not to wear the hat too tight. Carefully place the hat on your head in a manner that does not mess up your hair too much. No matter what you do, you're going to end up with a bit of an indention in your hair once you remove the hat, but there are a few things you can do to reduce or minimize that hat line.
My favorite technique is to take a small towel and soak it with the hottest tap water you can stand. Wring it out well and wrap it around your head. Keep it there for a minute or two. This will help "iron out" the indention left by the hat. Run the towel over your entire head (so the sides and back won't be the only damp parts) and then re-style. Of course, your hair will be damp after, but the hat hair will be gone. If there's a hand dryer in the restroom, you can also use that to dry your hair a bit -- just make sure the dryer has a nozzle that will turn upward. You'll look a little goofy, but a moment of goofy is better than walking around with hat hair all day.
If you don't have access to a towel, using your hands to dampen you hair a bit and brushing it upward for a few moments should help eliminate the hat head as well. A bit of styling product (a styling paste works well) can be used to restore body and get your hair back in place as well. You do carry styling product around with you, right?