Sonic is a really, really, really ****ing gay matchup that is in G&W's favor by about 65/35 or 7/3. I've played this matchup quite a bit, and if you play it wrong, you will get owned, but frankly, Sonic doesn't actually have what it takes to compete with G&W if he choses to play gay.
Shieldcamping works pretty well against Sonic. If he tries to spindash into you and follow up with an aerial and you shield, you can punish him with Up-B out of shield. Even if he chooses to stop short, he's not going to easily punish you either way if you just Up-B.
If he's in a corner, B-air and he pretty much can't do anything about it.
Homing Attack is punishable with Up-B out of shield.
His recovery just begs to be punished if he recovers from below. D-air or Smashes will own him if he goes from there. You can try to also skyrocket him with the U-air if you think he will go high IIRC, and U-air stall is good against him because his aerial mobility has horrendous acceleration.
Throwing out F-smash is fairly safe in this matchup since it can't be easily punished.
Sonic can't do a thing if you edgecamp right since you can drop down with a N-air and jump back to the ledge with an Up-B, and this will pretty much beat his attacks 100% of the time.
Most of your kills should come from either edgeguarding him, or punishing him has he comes down with a U-smash IMO.
Most of the time, he will kill you with F-smash or D-smash if you really really badly space an aerial, or with U-air near skybox if you are really really dumb and let him hit you with it.
From what I've seen, a lot of this matchup has a learning curve in understanding Sonic's mobility options. When you understand those better, timing the right attack to punish him becomes easier, and generally most other characters kill him earlier than he kills them (unless he gets an edgeguard KO).
In case you aren't aware, Sonic's number one quality is punishing. Just pointing out.