isatty is a function for checking this, and the -t flag of the test command makes that accessible from a shell script:
-t file_descriptor
True if file descriptor number file_descriptor is open and is associated with a terminal. False if file_descriptor is not a valid file descriptor number, or if file descriptor number file_descriptor is not open, or if it is open but is not associated with a terminal.
You can check if FD 0 (standard input) is a TTY with:
test -t 0
You can do the same for FDs 1 and 2 to check the output and error streams, or all of them:
test -t 0 -a -t 1 -a -t 2
The command returns 0 (succeeds) if the descriptors are hooked up to a terminal, and is false otherwise.
test is also available as the [ command for a "bracket test":
if [ -t 0 ] ; then ...
is an idiomatic way to write this conditional.