Let $b=\overline{a_1a_2....a_n}$ be an n-digit number. We can break $b$ into parts, as follows:
$$
b= \overline{a_{n-2}a_{n-1}a_{n}} + 1000(\overline{a_{n-5}a_{n-4}a_{n-3}}) + 1000000(\overline{a_{n-8}a_{n-7}a_{n-6}}) + \ldots
$$
Al you do now, is note that $1001=7 \times 143$ and $999999=7 \times 142857$. Similarly, you can prove that $10^{6n+3} \equiv -1 \mod 7$ (which means that $10^{6n+3}$ leaves a remainder of $-1$ (or $6$) when divided by $7$) and $10^{6n} \equiv 1 \mod 7$. Thus, when we take remainders above:
$$
b \equiv \overline{a_{n-2}a_{n-1}a_{n}} + (-1)(\overline{a_{n-5}a_{n-4}a_{n-3}}) + (1)(\overline{a_{n-8}a_{n-7}a_{n-6}}) + \ldots \mod 7
$$
Thus, you can see where the pattern comes from. I'll leave the rigorous details to you.
By the way, I have simpler rule for division by $7$:
Take your number $\overline{a_1a_2\ldots a_n}$, remove the digit $a_n$, subtract $2a_n$ from the rest of the number namely $\overline{a_1a_2\ldots a_{n-1}}$. repeat this until you get a small number. If this number is divisible by $7$, then your number is divisible by $7$.
Note: instead of subtracting $2a_n$, we can even add $5a_n$, and the result remains.
I will leave it to you to test this method out and to prove it.