I'm not a specialist of it, just a book I read years ago.
Basically: you wrote some code and didn't know how to make money of it, otherwise you would have.
If someone reuses your code (according to the license of your choice), there is a chance that you get something out of it. It may be recognition (something to add to your resume) or maybe a job offer or be paid for specific development as a contractor or just to help that firm's dev team make the most of your code.
So releasing your code publicly is an appeal for cooperation. Without doing it, well, nobody makes money out of your code but you don't either.
Classic example: A and B are two persons with no connection. You tell B you will get $1 if he/she accepts that A get $10. Otherwise both get nothing.
Most people prefer having nothing than having the extra $1.
If don't know how to monetize the code you wrote, well... game theory pleads in favor of being in cooperation mode. You may get only a small fraction at first of the profits but by cooperating, you're in the game. That is then up to you to make the relation evolve so that you get your fair share at some point.
The other answers display the variety of what people get out releasing their code. It varies a lot from a person to another.
The difficult part is to stay in an healthy relationship, e.g. where all parties are happy. If you can't stop complaining about being treated unfairly, that won't work. But the firm using your code should know as well its own interest in the relationship, so they should want you happy.