It does not work in general; it only works then the divisor is $1$, $2$, $5$, or $10$ (the reason being that these numbers divide $10$).
For an example where it does not work, consider $27$ divided by $7$. If we only looked at $7$ on $27$, we would conclude that the remainder is $0$. But the remainder is $6$.
The reason it works for $5$ (and it would work for $1$, $2$, or $10$), is that if you divide by $10$, the remainder is precisely the units digit (the rightmost, or least-significant digit). And dividing by $5$ is the same as first dividing by $10$ and then dividing by $2$, so you can just look at the last digit. Similarly with division by $2$ and by $1$.
Similarly, to find the remainder when dividing something by $4$, you could just look at the last two digits, because $4$ divides $100$, but the last digit would not be enough (for example, $15$ leaves a remainder of $3$, but $5$ leaves a remainder of $1$).
In general, if the divisor $q$ divides $10^n$, then the remainder is the same as the remainder of the last $n$ digits of the original number.
All of this becomes very clear when you learn about modular arithmetic.