Suppose your friend guessed x. And you told him to multiply 5 and then add 2 to it. So your friend got 5x + 2. Suppose your friend got 17. Now notice that if you do reverse operations on the number 17, you will get what your friend guessed.
So what's the last operation you told him to do? You told him to add 2. After adding 2, your friend got 17. So what was the number before adding 2? simple 17 - 2 = 15.
Now, how he got 15? By multiplying 5. Before multiplication, what was the number? Simple, 15/5 = 3. So your friend guessed 3.
You can also solve this problem by solving equation like Ax + B = what your friend got at last. But the above approach is much more simpler.