One of the strengths of open source is the open collaboration between developers. It is, for example, succeeded with Drupal to build a global community of hundreds of thousands of developers and users, contributing again to improvements of Drupal.
There are several open source licenses. Drupal and all of its modules and themes can be found under the GNU GPL 2.
The license does not force you to give away your code to others, but if you want to publish him, this must also be licensed under the GNU GPL 2.
Read more about Drupal and the GNU GPL 2.