Sonic's backstory isn't one story but several. Broadly speaking there's an American version, a European version, and the Japanese games, which never really committed to an origin tale at all.
In this continuity Sonic is a blue Mobian hedgehog, born on Mobius on day 162 of the year 3220, and the fastest creature in the universe. His full name is given as Olgilvie Maurice Hedgehog, and his fuel of choice is chili dogs. He isn't the sharpest thinker — his real flaw is leaping before he looks — but he makes up for it with heart.
He's the son of Jules and Bernie Hedgehog and the nephew of Sir Charles. All three of them, along with the family dog, were roboticized by Dr. Ivo Robotnik, also known as Julian Kintobor, though they retained their free will. His closest friends are Miles "Tails" Prower and Knuckles the Echidna, and he runs with the Freedom Fighters. Looking ahead to Mobius in 3243, Sonic and Sally marry and rule the Kingdom of Acorn as king and queen, raising two children, Princess Sonia and Prince Manik.
The European origin is considerably more tragic. Sonic explains to his animal friends that the Robotnik terrorizing them was once Dr. Ovi Kintobor, a scientist who genuinely wanted to save the world. Discovering the hidden lab before the transformation, Kintobor is startled to find a small, brown, prickly hedgehog who calls himself Sonic.
The two become friends, and Kintobor gives Sonic the tour, including the Retro-Orbital Chaos Compressor — the R.O.C.C. Held together by Containment Rings, the very rings Sonic collects in the games, the machine is powered by six Chaos Emeralds and built to soak up every trace of evil energy on Mobius. Without the seventh emerald, though, it can never stabilize.
Meanwhile Kintobor decides to measure just how fast his new friend can run, putting him on an experimental treadmill. Sonic breaks the sound barrier almost immediately, the treadmill explodes, and his dull brown quills turn a brilliant blue — the "cobalt effect," according to the doctor, who then builds him a pair of frictionless shoes to keep those speeds up.
The end comes over something absurdly mundane. Kintobor sends Sonic for a soda and a hardboiled egg, then fumbles the drink across the R.O.C.C.'s control panel, absorbing every ounce of stored evil in one go. The egg finishes the job: the gentle doctor's body twists into an egg shape, and Dr. Ivo Robotnik is born.