How does a Linux terminal work?

If you fire up a terminal and call an executable (assuming one that’s line oriented for simplicity) you get a reply to the command from the executable. How does this get printed to you (the user)? Does the terminal do something like pexpect? (poll waiting for output) or what? How does it get notified of output to be printed out? And how does a terminal start a program? (Is it something akin to python’s os.fork()? ) I’m puzzled how a terminal works, I’ve been playing with some terminal emulator and I still don’t get how all this magic works. I’m looking at the source of konsole (kde) and yakuake (possibly uses konsole) an I can’t get where all that magic happens.