Some attorneys take matters on contingency. Personal injury cases, some contract cases and other civil damages cases are some of the kinds of such matters sometimes taken on contingency.
i have never heard of a criminal lawyer taking a case on a contingency (only paid if they win). This is generally only common practice in cases dealing with money. (primarily civil or personal injury lawyers).
Rarely in a criminal case, a lawyer will agree to do it for free or next to free if it involves unique facts, notoriety or just for experience in a new branch of criminal law. Lawyers like Clarence Darrow took on many cases on principle and of course built a huge reputation doing so. But beware, not every lawyer willing to work for free is Clarence Darrow!