To avoid foreclosure, pay your mortgage on time each month.
If you can't do that, you can either work with the lender to change the terms of your loan OR sell the house. Both of those could have lots of complications, so instead of just guessing, how about you tell us more about your situation.
Do you homework BEFORE you buy. Know what you can realistically afford taking into account that there will be some major setbacks like maybe needing a new roof or septic system so whatever price range you are looking in you should have reserve cash for unexpected surprises. Just cause the bank employee tells you are good for X amount reality is the only person who really knows how much you can afford is you. You know your spending patterns as well as your savings patterns and if money is going out faster than it's coming in and you are always behind on bills or just barely making it from paycheck to paycheck and not able to save you aren't ready for home ownership no matter what that bank employee is telling you. Once you get a home and you fall behind enough that the bank starts talking foreclosure you are already too late to avoid it.