Good question, one posed by many in rough financial straights. The quick answer is, you can't, that is if you're truly broke. Bankruptcy is for people seeking to bring some sanity and solvency to their financial affairs, not for those just struggling to survive. So, food, shelter, that sort of thing, must always come first. As the saying goes, bankruptcy is not for paupers. However, if things are tough, but not truly dire (which is usually the case), and would seem to improve without the weight of debt, phone calls, lawsuits, etc., then this is how people tend to pay the couple thousand dollars a typical bankruptcy case costs (sometimes more, sometimes less). People arrange payment plans to get a case filed. They often stop making certain debt payments (like credit cards) in the interim to help raise the money. This is quite a popular strategy and works just fine in most circumstances. However, there are times when a wage garnishment order is about to enter or some other shoe is about to drop, and this just won't work. Those circumstances are difficult, but even in those cases solutions can sometimes be found if the money is not available from a family member, friend, or retirement fund. Moreover, it tends to be quite rare that people wait until the last minute like that to explore their options. So, in the normal case, being on a payment plan for two to three months before filing works quite well. You are able to have a lawyer to go to with questions and help with the pre-filing heat (usually phone calls from debt collectors) and have the guidance to set up a case correctly.
This is just a common way that people scrape together the money to file. There are other ways, like having some savings or a family member to help. I find that this is a popular question and this quick blog post reveals the usual answer.