Without knowing the exact circumstances, including the value of your property, the size of the mortgage, and your ability to make payments, no competent lawyer would answer this question. You would need to consult with a bankruptcy lawyer in person and answer these and other questions first. In general, bankruptcy does not always mean you will lose your house.
Most states allow you to claim a “homestead exemption,” and keep part of the value in your home. If your equity is less than your homestead, you may be able to keep it, but you will still have to pay the overdue amounts. In some cases, a Chapter 13 can allow you to spread out repayment of arrearages (the amount you are behind) by making payments over time, but you will have to make the regular monthly payments going forward, and you will have to confirm a plan. Bankruptcy may also be a good forum to raise any defenses to foreclosure based on your lender’s conduct or illegal terms of your loan.
If you want to keep your house in bankruptcy, you will either have to get current and reaffirm your debt in chapter 7 or you will have to satisfy your arrearage and keep current through chapter 13. Since you are in foreclosure, you probably can't afford to bring the loan current immediately. Chapter 13 can help but only makes sense if you would still have equity in your house after taking into account the current value of the house less the balance on the mortgage and further subtracting the amount you are presently in arrears. Absent significant equity, there is little economic reason to try to save your house. Many people have emotional attachment to their homes - understandable, but nof necessarily in their best economic interests.
If you file a chapter 7, the court per se doesn't take your house, but the court will allow the mortgage company to continue the foreclosure process. This does however protect you from having to pay any deficiency that could result from the foreclosure.
On the flip side, if you file a chapter 13, you will be able to keep your home if you can afford the chapter 13 payments that will get you caught up on the house. This would be your current mortgage payment, plus additional funds to get your arrearages paid in full in no more than 60 months.
You need to consult a bankruptcy attorney to determine whether you can afford a chapter 13.
The court never takes your house away. It is not the job of the court to do that. Only the secured creditor can do that by foreclosing or, in the case of a chapter 7 or chapter 11 trustee, the trustee can sell your house if there is sufficient equity for the bankrutpcy estate over and above your homestead exemption.
Most likely, the real question you want answered is what will happen to the home after you file for bankruptcy. If you have little or no equity for the benefit of the bankruptcy estate, then likely what will happen is that the secured creditor will file a relief from stay motion seeking an order that allows the creditor to complete its foreclosure sale. If you can't make the monthly payments that come due after the bankruptcy is filed, then the creditor likely will get relief and will complete the foreclosure.
If you have decent equity, and you are able to make your monthly payments after you file for bankruptcy, then the likely course is to file a chapter 13 bankruptcy which should allow you to keep your home so long as you can continue to make your payments.