People representing the platform (Epic, Steam, GamePass, etc.) choose games they want to give away and they reach out to the company handling the game's distribution (the game's publisher or studio). If the publisher/studio is amenable, the platform then sets up a deal with the game publisher/studio to "give" the game away.
This translates to a contract between the two parties. The platform typically agrees to terms like when the giveaway will occur, how long the game will be available, that the platform will pay the publisher/studio some agreed-upon amount per player who plays/claims the game, and usually a guaranteed minimum total payout to provide assurance to the publisher/studio that the deal will be worthwhile.
Got a burning question you want answered?