Foreclosures, and all the generic-types of involuntary transfers in the prior question (except government surplus auctions), occur because someone did not follow the rules or made unwise decisions.
A borrower did not make his or her payments on time. Someone did not pay his or her taxes. Someone else broke the law. The point is, there was a mess and it resulted in the involuntary transfer of real estate.
There are many traps and pitfalls when investing in foreclosure properties because while some of the previous owners’ messes are visible on the surface, there are other messes that you cannot see right away.
If you are knowledgeable, you can avoid most of the risk and minimize the fallout from the problems that cannot be managed in advance.