WordPress is Open Source Software and is licensed under the GPL.
Theme and plugin developers who create derivative works (i.e software that relies on WordPress to function) are encouraged to adopt the same licensing structure as WordPress, and many do.

The GPL allows users to use, share, modify and redistribute software as they please.
This enables us to redistribute software developed by authors such as WooCommerce and Easy Digital Downloads.

So in short: It is 100% legal to redistribute GPL code, and WordPress’ own creator Matt Mullenweg and most premium coders encourage it!