It's the same reason mechanics tell you do do preventive maintenance even though they make way more money swapping out large components.
A lawyer helps ensure the innocent don't get screwed by a mistaken witness, a vindictive prosecutor, or a corrupt cop, and that the guilty don't get railroaded with a laundry list of bullshit charges. Money spent on a lawyer will generally pay dividends when you consider the cost of false convictions and false arrest - losing your job, bills going unpaid while you're behind bars (and the interest, penalties, evictions, and reposessions resulting from them), damage to your credit, trying to find a new apartment with an eviction on your record, having to get a new car, etc. All these factors combine to make people who go to jail far worse off when they get out, regardless of their guilt.