Fuelled by established, public code repositories, developers increasingly leverage open source software in their projects. The benefits are that many of these building blocks are of high quality, well documented, used, tested, with contributions from, not only by the package authors, but also by collaborators in the wider coder community.
As an experienced developer with something to offer, I'll take some distinct functionality that I'm proud of, that could be useful to others. I'll simplify it, reorganize, modernize, generalize, document, test, package, and then publish it in my public repositories.
Keeping my code in a central repository helps me too. Apart from establishing credibility within the developer community, it keeps my code organized and I get to consume my modules in multiple projects. If I need to improve a component, or just fix a bug, I can do it in one place and have the updates propagate into all projects that depend on it.
Ultimately, sharing my work is a way of giving back; a way to belong, serve, and swim in a greater pool.