Why don't they do it now then?
Great question, I'll try and keep the word count down and should definitely caveat that this is purely based on my experience working in this space rather than absolute gospel - no doubt it's much more complicated than this, but this is my read. Short answer is the cost to compete and lack of inventory.
Sponsorships and Broadcast partnerships are delicately intertwined. So when sporting codes sign broadcast agreements with networks, in those agreements is that networks must approach the sporting codes sponsors first when they go to market with their sponsorship packages (eg: AAMI being a major sponsor of the AFL will always get the first opportunity to also be the major insurance broadcast partner with Channel 7). So because gambling companies (along with several other major brands) have deals with the codes, they get first bite of the cherry to buy ads. This doesn't completely soak up all of the inventory - but a fair chunk.
When it's time to renew those deals, those code level partners also get the first right of refusal when those partnerships go back to market, again locking out other brands from taking up those lucrative partnerships. Advertisers don't really get a chance to 'counter offer' here either - networks have to go to market at a reasonable price and once the deal is signed, its done. Most of the time in the NRL and AFLs case - other brands don't get a chance to pitch.
For the remainder of these spots - the lack of inventory (sport only goes for so long) and the relatively strong audience numbers mean costs are high. The average cost of a 30s spot pre-game at say 7:45pm (prime time) in sport can be 1.5-6+ times the cost than in regular general entertainment programs (depending on the fixture). Side note - this is why you typically see a lot of ancillary sport programs and long pre-game and post game shows. An NRL game might only go for 80 minutes +/- a few stoppages (ie: limited time for ads) - but if you can host an hour long pre-game and 30mins post game you significantly increase your ad inventory.
So if all of a sudden gambling companies can no longer buy the ad space they're entitled to (I'm assuming here that a future ad ban wouldn't stop them from still partnering with the NRL/AFL) you'll suddenly free up all of this inventory that other code level partners and general advertisers couldn't previously access.