In most words where /x/ changed to /f/ in English, the sound came after a round vowel. We can therefore describe the change as labialization: the influence of another sound pronounced with the lips caused the fricative /x/ to change into a fricative pronounced with the lips.
In all of the words you list with /f/, laugh, tough, rough and cough, you'll notice the word is spelled with the letter "u" before the "gh" digraph. This is not a coincidence. During the Middle English period, the /x/ sound caused a preceding vowel to develop a "high vowel" sound after it, either [i̯] or [u̯], and this is reflected in the spelling – you'll almost always see the letter "i" or "u" before a "gh" that represents historical /x/. (These were not full syllables, but glides.) Which glide was inserted depended on the preceding vowel. After the front vowel /e/, an [i̯] was inserted. For /i/, it seems the vowel was simply lengthened. After the vowel /a/, a [u̯] was inserted. After /o/ and /u/, it seems that [u̯] was inserted; however, there were complicated vowel shifts in these words that I still don't understand, so it's possible that in some cases the vowel developed to a long monophthong /uː/. In any case, the resulting vowel or diphthong ended in a rounded component.
After this process of glide insertion, the glides were later eliminated in various ways. In most cases, they merged with the preceding vowel, and the /x/ was later lost. This is what happened in words like "caught." But in other cases, the glide merged with the following /x/ instead. The [u̯] glide can be classified as a labial sound, made with the lips, and it imparted this lability to the fricative /x/, resulting in a labiodental fricative /f/.
It's believed that an intermediate step in this process was the shifting of the labial glide from before the fricative /x/ to simultaneously with it, or even after it.
In the development of English [f] from earlier round vowels followed
by [x], a crucial step was evidently a shift such as [ʷx] > [xʷ], in
which labialization is realigned with the end of the fricative.
Origins of Sound Change: Approaches to Phonologization, by Alan C. L. Yu, 2013
There is independent evidence that the glide disappeared in words where /x/ became /f/: words like "laugh" and "draught" have the "short a" sound rather than the usual sound associated with the digraph "au."
There is at least one word in Standard English where /x/ developed to /f/ in a different environment: "dwarf," from Old English dweorg, dweorh. Here the /x/ was after /r/, rather than after a diphthong ending in [u̯] or a rounded vowel such as /uː/. It's unclear why this happened; from what I can see, the regular development would be something like dwarrow (compare to barrow from Old English beorg, beorh; or marrow from Old English mearg, mærh). Interestingly, the Oxford English Dictionary actually does take the position that the /f/ in dwarf is regular; the entry for that word compares it to parallel pronunciations of /bɑrf/ (barf, bargh) for barrow and /bʊrf/, /brʊf/ for borough. As far as I know, though, those pronunciations are only used in a minority of dialects.
So how to explain /f/ in words like these? It's possible that the preceding /r/ counted as a labializing environment, but it's hard to tell. Alternately, the general tendency towards labial-velar shift that msam mentions may have contributed to pronunciations with /f/ even in some non-labial environments.