When you type commands into a terminal, you are really a long way from directly interacting with the operating system. (Which, in itself, mediates between you and the hardware by providing a number of useful services.)
To start with, you're looking at images painted on your screen by a "window manager". The window manager is just another program, not part of the operating system at all (at least in the case of Linux and other Unix-like systems.)
As the name implies, the window manager is primarily responsible for the arrangement of windows on the screen; the contents of the windows are produced by the various programs you are running, which instruct the window manager what to draw in the rectangles reserved for them.
One of the programs which you can run through the window manager is a so-called "terminal", more accurately described as a "terminal emulator". The name comes from the days when you interacted with your computer through a separate device with its own screen, which accepted simple commands from the computer sent over a serial cable (or through a telephone using a modem).
Linux, like many other Unix-like systems, does include a "console", which is a direct interface with the keyboard and screen. The Linux console does not have windows: it's a single array of characters which takes up the whole screen. Using the console bypasses the window manager and terminal emulator, but you're still far from interacting directly with the operating system.
What you are actually interacting with in both cases is a "shell", which is a simple program operating without special privileges. The shell doesn't even have a graphical interface; it just sends characters (and dome simple formatting commands) as though it were communicating with a real terminal. In Linux, that's mediated by an operating-system interface called a "pseudo-terminal" ("pty" for short).
The shell reads the commands you type, which are mostly requests to run other programs. Most shell commands do no more than find a program and run it with the arguments you provide. The shell does provide some facilities, like variable substitution, conditionals, and loops, which make it possible to write programs (or "scripts"). But these scripts are reinterpreted by the shell every time they are run.
At the bottom of this chain are the command-line utilities like ls
(which is an actual program you will find on your filesystem, probably at /bin/ls
). These programs do interact with the operating system. They are typically written in C (or some similar compiled language) and call specific library functions to request operating-system services.
That's a very brief and condensed view of what's going on when you type a terminal command. You can probably find more details by searching for the terms introduced above, although a more structured learning approach would be to pick up an introductory textbook.