The first script block adds ga.js into a script tag. That script gathers data about you and your browser, collecting information from where you came from (referrer information), where you're located, etc. All of this is collected on every hit.
The script creates a global _gat object and calls methods to that object. Some methods (like trackPageview) make a _utm.gif request every time they're loaded. (A _utm.gif request means that the script requests a 1x1 invisible gif file from Google's servers. Each request has all of the information appended at the end of it. Google's servers use the information about that request to process your data
That request, for example on stackoverflow.com, looks like this:
http://www.google-analytics.com/__utm.gif?utmwv=4.7.2&utmn=909339250&utmhn=stackoverflow.com&utmcs=UTF-8&utmsr=1920x1080&utmsc=24-bit&utmul=en-us&utmje=1&utmfl=10.1%20r82&utmdt=Newest%20'google-analytics'%20Questions%20-%20Stack%20Overflow&utmhid=456625342&utmr=0&utmp=%2Fquestions%2Ftagged%2Fgoogle-analytics&utmac=UA-5620270-1&utmcc=__utma%3D140029553.1672509655.1273785261.1282328140.1282335818.167%3B%2B__utmz%3D140029553.1282158995.159.95.utmcsr%3Dgoogle%7Cutmccn%3D(organic)%7Cutmcmd%3Dorganic%7Cutmctr%3Dforce%2520download%2520image%2520in%2520php%2520stackoverflow%3B&gaq=1
So, if you installed this script at the bottom of every page, every time someone loads a page, the script will embed, read the previous cookies, and send the updated information to Google's servers (via the _utm gif request).
The calculation of time on site is pretty piecemeal; it deduces time on page based on your browsing.
So, if you load index.html at 12:00:00 and send a _utm.gif hit to Google's server, and then at 12:01:30, you load about.html, it deduces that you've spent 1:30 on index.html.