It does have a lot to do with personality, but the Sorting Hat also looks at your legacy. Perhaps you appear to be a Hufflepuff prima facie, but the Sorting Hat knows your family has a history of Gryffindor, and it sees something deep inside you that makes you a Gryffindor. Take Neville for example. He is very much a Hufflepuff by nature, but by the end of the series, his bravery and courage is realized. That's why he was a Gryffindor, the Sorting Hat saw something in him that he was destined to become.
Personality is environmental though. You are like your parents, in one way or another. And in the wizarding world, your legacy is all you have. Your name. It is in a child's natire to want to appease and impress your parents. And the Sorting Hat gives you a choice, just as Harry tells Albus at the end of DH2. Scorpius may have the personality of a Hufflepuff, but maybe he has a destiny to be Slytherin or he just wanted his father to be proud of him and chose Slytherin.