In the early days of Hogwarts, the four founders hand-picked students for their Houses. When the founders worried how students would be selected after their deaths, Godric Gryffindor took his hat off and they each added knowledge to it, allowing the Sorting Hat to choose the students by judging each student's qualities and placing them in the most appropriate house. The student's own choices may affect the decision: the clearest example is the Hat telling Harry that he would do well in Slytherin in the first book, but ultimately selecting Gryffindor after Harry asks it not to put him in Slytherin.