How do I counter Sonic?
Sonic in SSB4 is quite a difficult match-up due to his movement versatility and options. The best advice I can give you is to read your opponent. Regardless of how good your opponent may be, they will most likely exhibit some habits which you can exploit. It's more about outsmarting the player versus finding a formula that will just grant you the "win" every time. However, there are some inherently bad match-ups in the game that are genuinely very difficult since your character has severe handicaps (e.g., Little Mac's recovery against any fast projectile characters).
I suppose some general advice for playing against Sonic:
His B move is the homing attack. If you time it right, you can side-roll (L trigger plus a direction) away just as the move is released. It will put Sonic in some idle (after the bounce) for you to capitalize on (e.g., you can use Ganon's Side-B to try set up for a mix-up game).
Don't try to play all-out aggressive against Sonic. A good player is a patient player that knows when to go in and when to stay back. If you play a defensive game, it will give you some time to decipher your opponent's playstyle and determine which options work best to counter. I play a Ganon secondary and find his neutral-A on the ground to be an effective tool against Sonic in close quarters, but you can't spam it; you need to time it when they're rushing in (though it isn't as effective in SSB4 as it was in Project M or Melee).
Go for the reads. Technical skill/knowledge and intuition are two components to any fighting game player. If you merely learn tech, you become a mechanical and predictable player. Relying solely on intuition takes you a lot further as you play a more adaptive style that you mold to fit your opponent, but can fall short if you don't know how to take advantage of a situation (i.e., going for non-optimal options). Both tech and reads need to complement each other.