Git stores snapshots: given any two snapshots of your choice, Git extracts the files you'd like compared1 to a temporary area2 and then runs a "diff engine" on the two files. The result of this engine is, or should be, a series of instructions that, when applied, will convert the left-side file to the right-side file.
Git has several built-in diff engines, which Git calls:
It can also run your chosen external diff engine.
1Or at least, the ones it wants to compare. If you are comparing files that are not actually in commits, they may already be extracted.
2Git mostly does this in-memory, but when running an external diff, really does use temporary files.