In one line: they have a list of all the exit nodes (something like that).
more detailed:
I have seen this post demonstrates how to detect a Tor connection in php
function IsTorExitPoint(){
if (gethostbyname(ReverseIPOctets($_SERVER['REMOTE_ADDR']).".".$_SERVER['SERVER_PORT'].".".ReverseIPOctets($_SERVER['SERVER_ADDR']).".ip-port.exitlist.torproject.org")=="127.0.0.2") {
return true;
} else {
return false;
}
}
function ReverseIPOctets($inputip){
$ipoc = explode(".",$inputip);
return $ipoc[3].".".$ipoc[2].".".$ipoc[1].".".$ipoc[0];
}
A good references explain what it does are available here:
Update:
From Tor offical doc that descirbes the TorDNSEL method that mitigates the drawbacks of the old method of testing exitnodes ip list:
It is useful for a variety of reasons to determine if a connection is
coming from a Tor node. Early attempts to determine if a given IP
address was a Tor exit used the directory to match IP addresses and
exit policies. This approach had a number of drawbacks, including
false negatives when a Tor router exits traffic from a different IP
address than its OR port listens on. The Tor DNS-based Exit List was
designed to overcome these problems and provide a simple interface for
answering the question: is this a Tor exit?
In ruby you have a cool Tor.rb gem that implements this technique:
Tor::DNSEL.include?("208.75.57.100") #=> true
Tor::DNSEL.include?("1.2.3.4") #=> false