This is done by recognizing some sort of signature that represents the virus or some program characteristic that indicates a virus may be present. Scanners allow you to check programs before execution. That is their main advantage. Scanners need to be regularly updated. Don't depend on an old scanner.
Some viruses attempt to defeat scanners by changing their code on the fly.
Current scanners attempt to analyze code on the fly as a way of countering this. Never run two scanners in a row without cold booting to clear memory between.