I believe there is a range of causes of headaches.
Sometimes it can be down to not drinking enough fluids; if the brain gets dehydrated (e.g. when someone’s been out on the booze all night) then it will hurt when it’s re-hydrated. At least that’s what I was told a hangover was!
Migraines on the other hand, I’m not sure about those, and I think I’d be right in saying they aren’t fully understood by neurologists either. They can be brought on by a range of things, stress, injury, apparently nothing at all.
I used to get a lot of headaches on one particular kind of medicine and I think hormones can be involved too.
Basically, there’s no one answer to why headaches ‘are’. I hope that’s not too annoying!!