Does anybody know how system restore really works?

I mean for example why would it be able to retain all of your documents but change other aspects of your system to what it was like previously.