(background: I've been freelancing for about twelve years, with several engagements that have resulted in open source contributions)
If you're freelancing, the general rule is that the customer owns everything you produce within the scope of the contract. This means you do not own the copyright, and therefore you can't open source the code. The specific phrase you look for in your contracts is "work for hire" ... and although IANAL, I believe this is the implicit legal relationship when someone pays you to produce something.
It's very difficult to get around that rule, or to outline specific exceptions ahead of time -- but you can change the incentives to encourage your client to contribute code to the open source community.
I offer to reduce my rates for any work we mutually agree to release as open source. I benefit by getting my name and work in broader distribution, my clients benefit by paying less for the work, and the open source world grows a little bit. It's a reasonable trade off for all parties, and even if most clients don't exercise that option, they appreciate the spirit of such an offer.
Where it gets hairy is when you're making changes to code that has been open sourced under a "viral" license, like the GPL. If that is the case, then you should inform your client that they are bound by the terms of that license -- that those changes *necessarily* become open source. Keep a copy of those emails. If your client decides that they're going to skip out on that obligation, you'll want to make sure your ass is covered if/when your client gets in trouble ...