Many dog hats have elastic or Velcro to fasten under the chin, but that might not be enough to keep a hat on a dog who isn’t fond of such fashion. At first, hats are strange things to dogs, so it might take some training for your dog to be open to wearing a hat.
To get your dog to allow the hat to stay on longer, slowly introduce the hat. Bring it out, let them sniff it. Continue doing so for a few days. When your dog is used to the hat, put it on and reward your furry friend for leaving it on.