On Linux and most UNIX based OSes, just printing the BELL character (Ctrl+G or '\a') will trigger a beep.
This is implemented through an ioctl() syscall, which is in your shell (bash, etc. support this). The syscall then goes through the kernel and is finally implemented by the pcspkr device, a kernel module. You can see pcspkr loaded with lsmod and unload it with modprobe -r or rmmod.
Don't even think about /dev/tty when thinking about beeps, because even headless programs that aren't bound to a TTY can produce a beep. Run strace -eioctl bash and in the bash prompt run echo ^a. You'll see that producing a beep is a simple but fairly specific ioctl() call (#include <sys/ioctl.h> and <linux/kd.h> if writing a C/C++ program to do this.)
References:
- http://tldp.org/LDP/lpg/node83.html
- http://www.linuxplayer.org/2010/04/beep-your-pc-speaker-in-linux