What part don't you understand. Client has a need, would like it to be solved for them. Consulting firm has seen multiple clients every year that have the same problem and the solution could be applied to each of them. Firm approaches Client about rights to the code and Client generally gets some consideration (usually in form of some free hours, as giving hours away is better than lowering your rate) and consulting firm gets to keep and resell the product or portions of the code. Support may be what is given away too, like 2 years free support or something rather than free hours. That is when it is done on the up and up.
In my own experience I also had taken a Client's idea and said I wouldn't charge them for the development because I wanted to own the product but that I would give them a discounted price, allow them to submit requirements and to be the early "beta" tester.
Sadly, what I have seen far more than the correct way is a Consulting firm develops a product for Client A and then keeps the code or some portion of it, and starts reselling it to other clients (or selling hours like they are developing it -- even worse). Which generally (unless contractually explicit as allowed) is a major violation and potentially illegal. But I had a lot of Clients come to me that had this happen to them, both by small boutique consulting firms and some of the largest firms. Sometimes I think it was sour grapes from the Client as it didn't really seem to have happened when we would look. But I lost track of how many times it had occurred in at least in some form. What was sad is firms wouldn't even try to cover their tracks, since we did a lot of ecommerce work we could see it literally in the javascript/html/css a lot of times, literally line for line.