When you calculate a Kolmogorov-Smirnov statistic you find the maximum distance between cdfs. In the case of the two-sample statistic, it's the maximum distance between two empirical cdfs.
At the extreme left (to the left of any data points), the distance between cdfs is 0, and at the extreme right (to the right of all data points) the distance is also 0. To find the largest of the distances in between those, at most you need to compute the distance when it changes. Here's an illustrative example:
As we can see the distance between cdfs only changes at the data points. As a result if the two sample sizes are $m$ and $n$, the largest number of changes we'd have to consider would be $m+n$. However, the very last change will be to $0$, so we don't need to check that one, making it at most $m+n-1$ places we need to consider the difference in values.
(It may be fewer than that in some situations - for example if there are ties in the data, there's only a need to consider the total number of distinct values.)
At an observation from the first sample, $F_1-F_2$ (note this is not presently $|F_1-F_2|$, it's a signed quantity) will increase by $1/m$ and at an observation from the second sample, $F_1-F_2$ will decrease by $1/n$.
In some cases it's possible to know that the largest $D$ identified so far will not be exceeded at the next change or even the next several changes, in which case it may in principle be possible in some particular situations to reduce the number of comparisons from $m+n-1$ to some smaller number.
However, to my knowledge actual implementations generally calculate the difference at every data point (often including the redundant calculation at the last observation of the combined sample).
As an example the implementation in R calculates the difference at all $m+n$ values; no attempt is made to try to reduce it (not least because it's likely to be slower to try to reduce that calculation than simply to do the calculation).