Well, Google stores all cached sites on a local (to google) database and the serves them up when you click the "Cached" button. I would imagine it then uses a string replace (see str_replace) function to replace;
[search word]
with;
<b style="color:[text colour]; background-color:[bg colour]">[search word]</b>
Simple. This can be altered to do any type of highlighting, including underlining, bolding or any other HTML highlighting.
HTH
Matt