But that's just some string, printed to the console. How does the actual mechanism work?
Well, what you see is not just tests being run, but an output of another program that runs the tests. A runner, or wrapper if you will. We can write one in bash ourselves
ls "foo" # we can another command instead of running tests
if [ $? -ne 0 ]; then
echo "Directory 'foo' does not exist."
else
echo "Directory 'foo' exists."
fi
And if you run it:
$ ./runner.sh
ls: cannot access 'foo': No such file or directory
Directory 'foo' does not exist.
We essentially have a program that runs another program, and inspects it's return code (bash holds the last run program's return code in $? var)
This is a standard across operating systems for program to return a status upon it's execution. The successful runs usually return 0 (I'm not sure if it's universal rule, though).
In Ruby (since you're asking about rspec, I'll asume this is what you're familiar with) you have https://ruby-doc.org/core-2.6/Process.html#method-c-exit to control (to an extent) what your program returns.