This is rather a gamble! They have to be careful about who they lend it to, and there are banking rules about how much banks should keep in cash so they can pay depositors who want their money out. As long as most borrowers keep up their repayments it works just fine.
But yes, if too many borrowers don't repay, the bank has a problem. This is the cause of the banking crisis of 2008 - banks had lent too much to people who couldn't afford to repay and the chickens came home to roost!
If that happens, then depositors could get scared and want their money out - and the bank doesn't have enough cash. There is a "run on the bank".
But normally, the bank lends sensibly, repayments happen, and depositors don't want their money out all at once. Then it works.