When a game is on Steam the publisher/developer can get as many free keys from Valve to distrubte however they want. Valve only takes the 30% cut of sales from a game sold directly on the Steam store.
They do it because if developers/publishers sell Steam keys on other stores or give keys away it will get those people to use Steam, and generate more money for them in the future because those people will buy things from the store. Because Valve is privately owned they can think more long term like that then other companies who are publically owned and have to worry about getting highter profits each quarter to make investors happy.
I don't know about GoG but I wouldn't be surprised if they don't charge anything for keys either.