It depends on the continuity. In the older comics, their world, Mobius, is a post apocalyptic earth where humans are scarce and animal characters (Mobians) rise up in their place. In some TV shows (Sonic X for example), Sonic and friends are just from another planet in deep space. In the current canon games and comics, Sonic and other animal characters have just kinda always been there just like humans.