Developers typically, increasingly, use a ton of open source software in their projects. Over time, these building blocks gets updated by the package authors and contributors. When we leverage their code, albeit in subtle ways, our own code gets improved and in a variety of ways we help each other.
I'll take some distinct functionality that I'm proud of, that could be useful to others's. In some cases the original code was developed years ago, then extended over time to fit the needs of various projects. I'll simplify it's use, reorganize, modernize, generalize, document, test, package, and finally share it in my public repositories.
Keeping my code in a public repository helps me too. Apart from establishing credibility in the developer community, it keeps my code organized, I get to consume my own modules. If I make improvements or fix a bug, all my projects easily get those updates.
Ultimately, sharing my work is a way of giving back; a way to belong, serve, and swim in a greater pool.