The Sorting ceremony is a yearly ritual performed at the beginning of each school year at Hogwarts School of Witchcraft and Wizardry. Its purpose is to assign first years to one of the four school Houses: Gryffindor, Hufflepuff, Ravenclaw, or Slytherin. The Sorting is held in the Great Hall before the start of term feast, after all students have arrived at school via the Hogwarts Express.
The Sorting Hat is placed upon a stool at the head of the Great Hall, whereupon it sings a song of its own composition about the four founders of Hogwarts and the qualities sought by their respective Houses (this song is different every year). The Sorting Hat also can give warnings to the students whenever it detects a need (it can be presumed that the Hat gets its information from the headmaster's office, as it lives there). New students are then summoned to the stool one by one in alphabetical order by last name, where they sit upon the stool and don the Hat.
Decision of the Hat
The decision of the Sorting Hat is generally considered to be final, although its decision can be influenced somewhat by the desires of the wearer. However, there are only two people who definitely know this. When Harry Potter was being sorted in his First year, the hat told him he would do well in Slytherin, but Harry asked to be placed elsewhere. Harry later told his son Albus Potter about the hat's ability to take the wearer's request into consideration. It is possible that Sirius Black also asked to be placed in Gryffindor, since he decides to break the tradition of his family being in Slytherin after consulting James Potter. It is also possible that Hermione Granger asked to be placed in Gryffindor, as she states that before deciding on Gryffindor, the Sorting Hat seriously considered putting her in Ravenclaw, and it is known that Hermione particularly wanted to be in Gryffindor (though she wouldn't have minded Ravenclaw).
Sorting Hat's song
The Sorting Hat's introductory song changes every year, presumably because, according to Ron Weasley, the Hat has nothing to do between Sortings except compose a new song.