I was interested in writing some home-brew analytic solutions and all the click tracking so far is really easy, however I was curious how GA gets the data showing how a user got to your site. Also as a followup I was wondering if it was possible to either write this functionality myself?
The site puts up some Javascript from Google Analytics. They track you using a cookie. They track where you come from because all popular browsers set the HTTP_REFERER header on the request to say where you're coming from.
There is an open source GA "clone" that you could look at: http://piwik.org/
GA uses javascript to gather information about the page viewed and send it to their server. As far as how the user got to your site, it looks at document.referrer for the first hit to your site with code on it.
You can certainly write it yourself using the referrer field as others have mentioned.
There are some advantages to using Google Analytics rather than a roll-your-own solution that you may want to consider before investing a lot of time in it. The biggest of these is probably AdSense integration, but the fact that Google use a GUID cookie to track everyone means that in theory at least they can also provide a much broader offering of data about your visitors. (In practice they don't use most of the data in this way, I assume for privacy/PR reasons).
On the other hand, there are benefits to roll-your-own solutions such as SEO and more control over your own site content. There's a pretty interesting post on how to roll-you-own affiliate program over at SEOMoz.