Hang it to dry immediately. Don't ball it up.
Dry it as soon as you can. That’s the only real answer.
Mildew needs moisture and time. A wet towel left crumpled in a beach bag or on the bathroom floor is a perfect breeding ground. Even a few hours in a hot car can do it.
If you can’t hang it right away, at least spread it out over a chair or the back of a car seat. If you’re packing up and the towel is soaked, give it a good shake, wring it out, and lay it flat (not rolled tight) in your bag. Rinsing with fresh water first helps — salt and sand hold moisture and accelerate mildew.
When you get home, machine wash it with a splash of vinegar or bleach if it already smells funky. Then dry it fully before folding and storing.
Mildew is a bacteria party — don’t let it get started.
