Can your landlord evict you if you declare bankruptcy? That depends on the circumstances. If you're not behind on your rent, your landlord may never have to know about your bankruptcy. As long as you keep paying your rent, it's not really his business. A landlord can't evict you just because you filed for bankruptcy.
If you are behind on your rent, however, the landlord is in a different position. If he's already completed the proceedings for eviction, the landlord can proceed to evict you, despite the bankruptcy. Some states do not allow you to challenge this procedure. In states where you can challenge it, the proceedings are fairly onerous: you must file a paper stating that state law gives you the right to tenancy if you pay all the back rent, and immediately pay any current rent that is due. Then you have 30 days to pay all the back rent that you owe. If you don't comply with these regulations, then eviction proceedings can continue. Note, too, that this doesn't apply in the event that the owner can prove you've been doing drugs on his property or damaging it.
If the owner hasn't yet filed for eviction, you're in a much stronger position. Once you file for bankruptcy, the court imposes an automatic stay, which prevents the landlord from evicting you. The landlord can, however, apply to the court to lift the stay. In this case, eviction proceedings could begin in 2-4 weeks. You can use that time to look for a new place. Also, remember your rights during this time: the landlord cannot lock you out or remove your property until he gets a court order; he can't barge in and he can't threaten you. The sheriff can serve eviction papers, but she can't arrest you.
If the landlord doesn't apply to the lift the stay, you will have the length of the bankruptcy proceedings before eviction proceedings resume. Once again, we have the 2005 bankruptcy law to thank for this tilt of the law in favor of the creditor against the debtor. The law specifically allows for a 'fast track' proceeding to make evictions easier during bankruptcy.
Note, however, that filing for bankruptcy can still be helpful if you're behind in your rent. If you owe back rent, that is included as a part of your unsecured debt – to be discharged in a Chapter 7 bankruptcy, or paid out over time or partially or fully discharged in a Chapter 13 filing. Any rent that comes due after you file for bankruptcy won't be included in the petition, however, and you will remain responsible for it.
As a final point, there are a few rare cases where your landlord might become involved in your bankruptcy even if you're current on the rent. If you paid a rent deposit when you moved in, you have to list it as an asset. Unless it's an extremely large security deposit, however, it's most likely exempt and the trustee won't bother with it. In addition, if you've filed for Chapter 13 bankruptcy, the trustee will examine your lease. Most likely he will approve it; moving is expensive and it's not in your creditor's interest to have you shelling out money to find and move to a new place. You'd only be forced to move in the unlikely event that you're paying way over market value for your apartment and there are an abundance of cheap places available.