Every diff tool answers the same question: what is the smallest set of edits that turns text A into text B? Everything you see, the removed lines, the added lines and the highlighted words, comes from that answer.
Start with what stayed the same
It is easier to find what changed by first finding what did not. Take two lists of lines:
A: apple, banana, cherry, date
B: apple, cherry, date, elderberry
The longest sequence of lines that appears in both, in the same order, is apple, cherry, date. That is the longest common subsequence. Whatever is not part of it is an edit: banana was removed from A, and elderberry was added in B.
Why "in the same order" matters
A diff does not look for lines that merely exist in both texts. It looks for lines that appear in the same relative order, because order is what lets it say that something was removed in one place and added in another, rather than that everything changed.
Fewest edits, not the only answer
Often there is more than one way to reach the same minimum number of edits. A common approach, described by Eugene Myers in 1986, searches for the shortest edit script efficiently when the two texts are similar, which is the usual case. Different tools break ties differently, which is why two tools can mark different lines for the same pair of files.
From lines to words
Line-level results are good at showing where a change is. To show what changed inside a modified line, a second pass compares the two lines word by word, and the differing words are highlighted.
What a diff cannot know
A diff compares text, not meaning. It cannot tell that a renamed variable is the same variable, or that two JSON objects with reordered keys are equal. That is why normalising first, such as formatting code or sorting keys, makes diffs far more useful.
Questions
Why do two diff tools sometimes show different results?
A pair of texts can often be turned into each other in more than one way with the same number of edits. Tools choose differently, so the marked lines can differ while both are correct.
Is a diff the same as a merge?
No. A diff describes the difference between two texts. A merge combines changes from two versions, which also needs a common ancestor to decide who changed what.