To keep the sun, rain, snow off their heads
Why do cowboys wear hats
To help protect their heads and eyes from sun, rain etc. Remember they didn't have sunglasses back then.