Person A, and leaves it at the broker. Person B borrows not from person A, but the broker who. Person C buys the stock and again leaves it in the same broker. In the database there is 10 shares to person A, -10 to person B, and 10 to C. If A wants his stock back, then the broker just takes the stock from the pool not from person B.
Since there are a lot of people with stock at the broker there is no problem to shuffle around, it is all the same stock and an entry in the computer.
This of course leads to the real issue: you can buy stock and not leave it with your broker. This has been done, but only rarely (and not in this case)