Terminal emulators use ANSI escape sequences to --amongst other things like controlling the cursor’s position-- read the desired text and background color when printing output.
$ echo -e "\033[31mred\033[m" # Prints "red" in red.
While most terminal emulators, including Apple’s Terminal.app, support true color now, most utilities use one of the main sixteen colors (black, red, green, yellow, blue, magenta, cyan, white, and a high intensity or bright version of each). For example, git shows diffs with additions in green and deletions in red, and most testing frameworks print green dots and red "F"s for failures.