Because paying off a loan early means that loan has had less time to generate interest, which means the bank gets less money.
On an extreme example, imagine taking out a loan, then you pay it all back the next day. The loan has earned no interest, but the bank had to spend a bunch of time and money to get the loan set up.
Of course, early payment penalties are still pretty scummy. However, it does make sense why a bank would have one.
I don't know if they're scummy. When I buy a typical corporate bond I expect that the company will pay until maturity. If I buy a callable bond wouldn't I expect to be compensated for the one sided exposure to interest rate risk I'm exposing myself to (rates go up I lose, rates go down I lose)?
With consumer debt we have different expectations of what's fair, but consider the bank's risk profile here - if interest rates go up those outstanding mortgages are taking them for a ride, and if they go down the customers refi... seems natural they'd want to minimize prepayments.
In my view, the chance that a loan will be paid off early can be worked into the risk calculation that the bank uses for coming up with the interest rates and fees that they are willing to offer, and also for computing the cash value of the mortgage on the secondary market. At the end of the day, the bank just wants to know that they can sell the loan for more than it cost to originate it.
An issue with consumer debt is that a proliferation of fees and "fine print" make it confusing for folks to understand what they're signing up for, and to do comparison shopping on loans.
Traditionally, folks were advised to be wary of prepayment penalties because of a widespread strategy to prepay a loan if possible. When interest rates were high, this was like an investment with a guaranteed percentage return.
For instance, my family took out a loan with a particular payment, but we paid more than the minimum each month because we could afford it. On the other hand, being able to drop back to a lower payment was a kind of safety net in case something happened to one of our jobs, or something like that.
On an extreme example, imagine taking out a loan, then you pay it all back the next day. The loan has earned no interest, but the bank had to spend a bunch of time and money to get the loan set up.
Of course, early payment penalties are still pretty scummy. However, it does make sense why a bank would have one.