"My head hurts."
To expand on Rhiaaaaannon's explanation - の adds an "explanatory" tone to a statement, making it clear that it's either giving or seeking an explanation for something. For example, if someone asked you, "Why are you frowning?" you could answer 頭が痛いんです, making it clear that you're answering the question through your statement, rather than just making a totally separate statement about how your head feels.
I like to think of it in English as being analogous to adding "it's that..." before a sentence. My understanding is that this is what's literally happening in Japanese, too - the の is nominalizing the phrase, turning "my head hurts" into something a little like "(it's that) my head hurts". The second one sounds awkward in English, ofc, but I think it helps to capture the explanatory/nominalising tone that の/ん conveys.
Native speakers, people more clued up than I am on this subject - please feel free to correct me if I've grossly misunderstood what's going on here!
ん is like a contraction of の?. So this sentence more formally would be あたまがいたいのです。?の and ん, when used like this, don't really serve any grammatical purpose and are just kind of there to make the sentence... flow I guess? But nobody really says あたまがいたいのです in speech so あたまがいたいんです is more natural.