One of the feilds passed when your browser calls a web page, is the address of the refering page. You can pull the referer data into a script and parse it for information. Since sites like google include the search terms in their pages URL (in the query string) you can parse that out of the referer address.