There is a lot of investor pessimism surrounding the lending industry right now, and it certainly makes sense. After all, soaring interest rates have caused demand for loans to decline, and if a recession hits (like most experts are predicting), it could cause a rise in loan defaults.

However, Ally Financial (ALLY 6.73%) just showed investors that things might be going better than anticipated. The bank recently reported its year-end 2022 results, and shares soared by as much as 20% the day after the announcement. Here's a rundown of Ally's fourth-quarter numbers, why the market reacted so favorably, and why the stock could still be a great buy for long-term investors.

Ally handily beat expectations for the fourth quarter

Let's start with the headline numbers. Ally beat expectations on both the top and bottom line. The bank generated $2.2 billion in revenue, $150 million more than expected, and $1.08 in earnings per share (EPS) was 8% better than analysts had been expecting. And it's worth noting that expectations were likely even worse right before the report, as Discover (DFS 3.65%) reported disappointing results and high charge-off rates the day before.

Looking beyond the headlines, Ally originated $9.2 billion in auto loans during the quarter at an average yield of 9.57%, a staggering 260 basis points higher than a year ago. And impressively, net financing revenue was slightly higher year over year, despite the challenging economic conditions.

Ally also did a great job of returning capital to investors and taking advantage of the decline in its stock price. In fact, Ally spent $1.7 billion on repurchases in 2022 -- that's 18% of its entire market cap. And that is in addition to the roughly $350 million the bank stock paid in dividends.

An extremely profitable business model

If you aren't familiar, Ally was spun off from General Motors (GM -0.05%) after the financial crisis, so it shouldn't be a surprise that its primary business is auto loans. About 77% of the bank's loan portfolio is automotive in nature.

However, it's important for investors to realize that the company has evolved into a full-featured online bank with a massive deposit platform. The bank has $137.7 billion in retail deposits, providing a low-cost source of capital for nearly all of its $146 billion in loans.

Here's why that's important. Because deposit interest rates are much lower than other types of financing, Ally's average cost of funds in 2022 was just 1.71%. Meanwhile, the average yield of its new auto loans for the full year was 8.24%. Even with administrative costs and a reasonable default rate, it's not hard to see how this combines for a huge profit margin. In fact, Ally's net interest margin of 3.65% in the fourth quarter is one of the best in the banking industry.

Should investors worry about a rise in defaults?

Ally set aside $480 million during the quarter, more than double than it did in the fourth quarter of last year, but this is still largely in line with pre-pandemic norms and isn't a cause for alarm.

For the full year, Ally's auto loan portfolio saw a 97-basis-point (0.97%) annualized charge-off rate, but this had increased to 166 basis points (1.66%). However, this isn't cause for alarm. This is a bit higher than the 1.33% default rate Ally saw in 2018, but the bank has about 3.6% of its loan portfolio in reserves. The situation is certainly worth watching, but the charge-off rate is still quite manageable.

Ally could still be a great stock for patient investors

Despite the post-earnings pop, Ally still looks extremely attractive from a long-term perspective, especially if you believe the fears of a spike in defaults are overblown. After all, Ally trades for just over 5 times trailing-12-month earnings, and for a 12% discount to its book value per share. The stock is still 40% below its 52-week high.

To be sure, Ally isn't without risk. However, with an extremely profitable business model, an excellent history of returning capital to shareholders, and a cheap valuation, Ally is a bank stock that could pay off very well for patient long-term investors.