Demand upfront payment. ;)
Failing that, make the quantum of unpaid work as small as possible. Offer them the first three hours on credit, but make them pay a bill before they get a second three hours.
Write your contracts to specify several intermediate payments. This is how, e.g., construction contractors work: one third up front, one third at the midpoint of work, one third at the end. Or something like that.
Write contracts that involve writing code and demonstrating it to the client, but stipulate that you won't deliver the final product until after you're paid.
Keep them on a very regular schedule. Don't go very far out on a limb: Don't ever let yourself get several months ahead of the paycheck. This is particularly important with the first paycheck: Don't work too much before sending the client some sort of bill -- even if it's just a partial bill -- and establishing that they will pay. For all you know, your client is a mid-level employee without signing authority who cannot pay you without permission from a higher-level manager in a rival department. Or something.
But, of course, past performance is no guarantee of future results. Customers run out of money. Customers turn over their personnel. Customers have internal reorganizations. Trust, but verify.