[I'm just wondering how they work...]
Well, that's not exactly something that can be explained in 'just' five minutes - you know, kind of out of like say - 'mild curiousity'.
How GUI aplications work was the culmination of the lifetimes of study/theorizing on the part of computer scientists on the cutting edge of their discipline back in the late 1970's. Their work came to fruition at the Xerox Palo Alto labs in the late seventies and early eighties. Apple picked up on it first. Then Bill Gates and Microsoft.
Highly renowned writer Charles Petzold introduced Windows GUI programming to C programmers back in the late 1980s with his famous 'Programming Windows' books. That is how most Windows developers learned GUI programming. Petzold's books were aimed at professional C programmers, that is, people not just learning to program, but rather folks who were already very competant professional programers who were masters of the C programming language. The nature of the subject was so difficult & complicated that Petzold stated it would take such a aspiring professional C programmer six months of hard study to become somewhat competant at Windows programming.
The situation with Linux/Unix GUI development is the same - just different names involved - mostly from MIT I believe.
So, if you are just mildly curious, I'd just forget it.