Someone has to hand out cash to the seller. Even if no physical money changes hands (and I've bought a house; I can tell you a LOT of money changes hands at closing in at least the form of a personal check), and regardless of exactly how the bank accounts for the actual disbursement of the loan, the net result is that the buyer has cash that they give the seller, and are now in debt to the bank for least that amount (but, they now have a house).
Now, the bank probably didn't have that money just sitting in its vault. Money sitting in a vault is money that is not making more money for the bank; therefore most banks keep only fractionally more than the percentage of deposit balances that they are required to keep by the Feds. There are also restrictions on what depositors' money can be spent on, and loans are not one of them; the model of taking in money in savings accounts and then loaning it out is what caused the savings and loan collapse in the 80s. So, to get the money, it turns to investors; the bank sells bonds, putting itself in debt to bond holders, then takes that money and loans it out at a higher rate, covering the interest on the bond and making itself a tidy profit for its own shareholders.
Banks lose money on defaults in two ways. First, they lose all future interest payments that would have been made on the loan. Technically, this isn't "revenue" until the interest is calculated for each month and "accrues" on the loan; therefore, it doesn't show on the balance sheet one way or the other. However, the holders of those bonds will expect a return, and the banks no longer have the mortgage payment to cover the coupon payments that they themselves have to pay bondholders, creating cash flow problems.
The second, and far more real and damaging, way that banks lose money on a foreclosure is the loss of collateral value. A bank virtually never offers an unsecured "signature loan" for a house (certainly not at the advertised 3-4% interest rates). They want something to back up the loan, so if you disappear off the face of the earth they have a clear claim to something that can help them recover their money. Usually, that's the house itself; if you default, they get the house from you and sell it to recover their money.
Now, a major cause of foreclosure is economic downturn, like the one we had in 2009 and are still recovering from. When the economy goes in the crapper, a lot of things we generally consider "stores of value" lose that value, because the value of the whatzit (any whatzit, really) is based on what someone else would pay to have it. When fewer people are looking to buy that whatzit, demand drops, bringing prices with it. Homes and real estate are one of the real big-ticket items subject to this loss of value; when the average Joe doesn't know whether he'll have a job tomorrow, he doesn't go house-hunting. This average Joe may even be looking to sell an extra parcel of land or an income property for cash, increasing supply, further decreasing prices. Economic downturn can often increase crime and decrease local government spending on upkeep of public lands (as well as homeowners' upkeep of their own property). By the "broken window" effect, this makes the neighborhood even less desirable in a vicious cycle.
What made this current recession a double-whammy for mortgage lenders is that it was caused, in large part, by a housing bubble; cheap money for houses made housing prices balloon rapidly, and then when the money became more expensive (such as in sub-prime ARMs), a lot of those loans, which should never have been signed off on by either side, went belly-up. Between the loss of home value (a lot of which will likely turn out to be permanent; that's the problem with a bubble, things never recover to their peak) and the adjustment of interest rates on mortgages to terms that will actually pay off the loan, many homeowners found themselves so far underwater (and sinking fast) that the best financial move for them was to walk away from the whole thing and try again in seven years.
Now the bank's in a quandary. They have this loan they'll never see repaid in cash, and they have this home that's worth maybe 75% of the mortgage's outstanding balance (if they're lucky; some homes in extremely "distressed" areas like Detroit are currently trading for 30-40% of what they sold for just before the bubble burst). Multiply that by, say, 100,000 distressed homes with similar declines in value, and you're talking about tens of billions of dollars in losses. On top of that, the guarantor (basically the bank's insurance company against these types of losses) is now in financial trouble themselves, because they took on so many contracts for debt that turned out to be bad (AIG, Fannie/Freddie); they may very well declare bankruptcy and leave the bank holding the bag. Even if the guarantor remains solvent (as they did thanks to generous taxpayer bailouts), the bank's swap contract with the guarantor usually requires them to sell the house, thus realizing the loss between what they paid and what they finally got back, before the guarantor will pay out.
But nobody's buying houses anymore, because prices are on their way down; the only people who'd buy a house now versus a year from now (or two or three years) are the people who have no choice, and if you have no choice you're probably in a financial situation that would mean you'd never be approved for the loan anyway. In order to get rid of them, the bank has to sell them at auction for pennies on the dollar. That further increases the supply of cheap homes and further drives down prices, making even the nicer homes the bank's willing to keep on the books worth less (there's a reason these distresed homes were called "toxic assets"; they're poisonous to the banks whether they keep or sell them).
Meanwhile, all this price depression is now affecting the people who did everything right; even people who bought their homes years before the bubble even formed are watching years of equity-building go down the crapper. That's to say nothing of the people with prime credit who bought at just the wrong time, when the bubble was at its peak. Even without an adjusting ARM to contend with, these guys are still facing the fact that they paid top dollar for a house that likely will not be worth its purchase price again in their lifetime. Even with a fixed mortgage rate, they'll be underwater, effectively losing their entire payment to the bank as if it were rent, for much longer than it would take to have this entire mess completely behind them if they just walked away from the whole thing, moved back into an apartment and waited it out. So, these guys decide on a "strategic default"; give the bank the house (which doesn't cover the outstanding balance of course) and if they sue, file bankruptcy.
That really makes the banks nervous; if people who did everything right are considering the hell of foreclosure and bankruptcy to be preferable to their current state of affairs, the bank's main threat keeping people in their homes is hollow. That makes them very reluctant to sign new mortgages, because the risk of default is now much less certain. Now people who do want houses in this market can't buy them, further reducing demand, further decreasing prices...
You get the idea. That's the housing collapse in a nutshell, and what banks and our free market have been working through for the past five years, with only the glimmer of a turnaround picking up home sales.