It’s a blight, a thoroughly tough plight, enough to make you want to fight … or laugh. There are so many ways to pronounce, or not pronounce, the English "gh," almost none of which have anything to do with the usual "g" or "h" sound. Why is it there to begin with?
Once upon a time, the "gh" did stand for a specific sound, one we don’t have in English today, except in interjections of disgust like blech. That back-of-the-throat fricative (written as /x/ in the International Phonetic Alphabet) is found in German, and if you look for the German counterpart of English "gh" words, you will often find the sound there: light ... licht, night ... nacht, eight ... acht, high ... hoch, neighbor ... nachbar, though ... doch.
So when you see a "gh," it usually means that it was pronounced with the blech sound in Old English, when our writing system was first developed. Early scribes had to adapt the Roman alphabet to English, and since Latin didn’t have the /x/ sound, they used "h" or a non-Roman character called a yogh (ȝ). Eventually, during the Middle English period, they settled on "gh."
By that time the pronunciation was already changing. The sound turned into /f/ or was dropped entirely. The Great Vowel Shift was underway and many parts of the language were in flux, but by the time the shift was complete, the printing press had stabilized the writing system, and the "gh," pointing back to an earlier English, was here to stay.
Not all examples of English "gh" can be traced back to the /x/ sound. The word-initial "gh" of ghost and ghoul came from the habits of Flemish typesetters. Words borrowed from Italian like spaghetti and ghetto just stuck with Italian spelling conventions.
And there are some words that show how "gh" took on a life of its own in English, words that came into the language long after Old English and never had a /x/ sound in them. Delight and sprightly were modified under the influence of light and right. Sleigh was made to look like weigh, perhaps to avoid looking like slay. Haughty was modeled on words like taught and aught, because, well, doesn’t that look more haughty than hawty? Like it or not, "aught" now stands for a specific pronunciation, with a rounded vowel, that really can't be spelled any other way (at least in dialects without the caught-cot merger). Is taught the same as tot or tawt? I think naught.