`touch` calls the `utimes` system call to set the file's modification time and its access time. On some systems, instead of `utimes`, it opens the file and then sets the file times through the descriptor, e.g. with `utimensat` under Linux.
You can see how `touch` works on your system by looking at the system calls it makes. Under Linux, use strace, e.g. `strace touch -d '1 hour ago' foo`.
Where to find the source code depends on your operating system. The GNU version is in coreutils, there's a version in the main source tree of any BSD, there's a version in BusyBox, in Minix, etc.