There are different types of virus detection. Some of the different techniques they use are
1) Look at binary makeup of file for match or partial match in database of known viruses and trojans (most common technique)
2) Watch what program does and see if it ever does anything similar to viruses/trojans
3) Analyze program code (sometimes disassemble program code) and look for malicious things. This is often very difficult and usually only advanced detection programs do this.