When students are sorted the sorting hat looks into their minds and sees what characteristics they have and what potential they possess. THey are only 11 when this happens.
Is it possible that a persons character may change whilst at school?
Plus, the hat must have some percentage error. Even if it is 1%, that makes 10 people in the school incorrectly sorted.
Besides, talent and potential are nothing without making the right choices and working hard to achieve your potential. Wormtail did become an animagus, which suggests a certain amount of bravery and a little talent as it is difficult and illegal. Naturally, compared to James, Sirius and Lupin he is a little lacking, but they were the best in their year.