There's nothing hidden in your head
The Sorting Hat can't see,
So try me on and I will tell you
Where you ought to be.
-- The Sorting Hat (PS7)
"You're worth twelve of Malfoy ...The Sorting Hat chose you for Gryffindor, didn't it? And where's Malfoy? In stinking Slytherin."
-- Harry to Neville Longbottom (PS13)
"You've been wondering whether I put you in the right House ...Yes ... you were particularly difficult to place. But I stand by what I said before - you would have done well in Slytherin -"
-- The Sorting Hat and Harry (CS12)
Each year when the First Year students arrive at Hogwarts, they undergo a ceremony to sort them into one of the Four Houses: Gryffindor, Slytherin, Ravenclaw, or Hufflepuff. It is done through the use of the famous Hogwarts Sorting Hat (PS7).
A stool is placed in front of the students sitting at tables in the Great Hall, and the Sorting Hat is placed upon it. Usually it sings a rhyming song, either describing the sorting process or the ceremony itself (PS7), or giving a warning about House unity if Hogwarts is in danger (OP11). When that is over, each new student is called forward in alphabetical order by their last names. A professor – usually Minerva McGonagall – puts the famous Hogwarts Sorting Hat on the student’s head, and it looks into their mind and mulls over their placement (PS7).
While the Hat is on the student’s head a sort of back-and-forth conversation goes on in their minds. The Hat tells the student where they would do well, but also seems to take the First Year’s own wishes into account, since usually they get the House they want. Hermione said on the train to Hogwarts that she wanted to be in Gryffindor because it was Dumbledore’s house, and while the Hat kept insisting on Ravenclaw, she eventually got Gryffindor. Ron picked Gryffindor to be like the rest of his family, while Draco Malfoy wanted Slytherin for the same reason, and they both were sorted that way. With Harry, the Hat offered to put him in Slytherin first because he had such a close connection to Voldemort through his lightning-shaped scar. But by then Harry had met Draco and disliked him intensely, so he insisted on “Not Slytherin” until the Hat put him into Gryffindor. Neville Longbottom, who had little confidence in himself, argued against being put in Gryffindor and the Hat took a while to decide whether to put him there or in Hufflepuff. In that case, the Hat won out and Neville was placed in Gryffindor.
Very rarely a “Hatstall” occurs in which the Sorting Hat takes a long time to decide where to place a student. A true Hatstall has only happened twice in Hogwarts history: once with Minerva McGonagall (between Ravenclaw and Gryffindor), and once with Peter Pettigrew (between Gryffindor and Slytherin) (Pm).
Once the ceremony is over, the Sorting Hat’s decision is final. No one ever switches houses, though they may still have an affinity for another house, as Herbology-loving Neville has with Hufflepuff.
The Sorting Hat was not Rowling's first idea for the way students would be placed in their houses. Two other methods she considered were some type of magical machine, or statues of the Founders who would choose students some way. She explained on Pottermore how she eventually settled on the hat:
I wrote a list of the ways in which people can be chosen: eeny meeny miny mo, short straws, chosen by team captains, names out of a hat - names out of a talking hat - putting on a hat - the Sorting Hat (Pm).