Two different things get called the same name, so start with the strict meanings. In a subject-to purchase the seller signs a deed to you and you record it, so you own the property. The loan stays exactly as it is, in the seller's name and on the seller's credit, and you send the payments to the servicer. The lender isn't asked for permission and nothing about the loan terms changes. In a loan assumption the lender approves you as the new borrower, you qualify, and the seller is released from the debt. Creative finance people often say "assumption" or "sub2" loosely when they mean subject-to, so ask which one someone means before you sign anything.
It's legal in the sense that an owner can sell what they own and a deed can be recorded. Almost every mortgage also has a due-on-sale clause, which gives the lender the right to demand the entire balance when the property changes hands. That right doesn't fire on its own, and lenders often do nothing while payments keep arriving on time, but it's there. You need a real answer for how you'd pay off the balance if it ever got called.
Two things trip up first-timers here. The seller stays responsible for a loan they no longer control, which has to be spelled out in writing and actually understood by them, not just signed. And the county treats this as a sale like any other, so recording fees and any transfer tax apply, and both of those differ by state. Have an attorney licensed in the property's state draft the documents rather than pulling a form off a course.