The Sorting Hat is one of the Relics of Hogwarts. It is a simple wizard's hat which was enchanted to have life and sentience by the four founders of Hogwarts School of Witchcraft and Wizardry. The hat's primary duty is to sort the students of Hogwarts into one of the four houses based on its understanding of them after glimpsing their minds with advanced legilimency powers. Once the Sorting Hat has made its decision, it is final.
In 510 A.D., the founders of Hogwarts, concerned with the future of the school once they died, wondered how best to sort students. For the previous four years, they merely used their own legilimency to gaze into the minds of their students and determine which student should belong to which house. However, that would not be feasible forever.
Godric Gryffindor, whose wizard hat had fallen off his head, took inspiration to create and enchant a sentient hat to determine which student would be sorted where. All four founders were accomplished Mage Artificers, and they poured their collective knowledge together to create the hat. Each thread was imbued with magic to grant it life and consciousness. Their efforts worked better than any magical object in history: the Sorting Hat was not only fully sentient, but it had a soul of its own.
None of the founders understood how they accomplished what they did, and none of the founders ever attempted anything like it ever again. Still, the Sorting Hat was alive, and it held the ability to scan into the very depths of its wearers' minds and souls. Through this process, it was able to determine which house best suited each student based on the entirety of their character at the time of the sorting.
Fearing the possibility of knowledge of their handiwork getting out, and of the repercussions should people discover the Sorting Hat was alive, the four founders placed the Fidelius Charm on the Hat's purpose and made the Hat itself the Secret Keeper. That way, it would reveal its purpose to every new class of students, and yet those students would never be able to tell anyone who did not already know. This, they felt was the best way to keep both the Sorting Hat's existence in their school and its sentience, from becoming well-known.
Powers and Abilities
The Sorting Hat is extremely magical. It is imbued with vast legilimency capabilities, soul gazing spells, rational thought, and the cumulative knowledge and belief systems of the Founders at the time of its creation. With these skills, the Sorting Hat sorts each student of Hogwarts into their respective houses.
The Sorting Hat is also the Secret Keeper of the Sorting at Hogwarts. By this, it means that only the Hat can reveal exactly how the students are sorted into their houses. To that end, he hat chooses to sing a song every year to announce to the new students how exactly they will be sorted.
Over the course of fifteen hundred years, the Sorting Hat has never been wrong. Certainly, there are instances where a Gryffindor is cowardly or a Ravenclaw unintelligent, but the Sorting Hat sees not just actions and potential, but aspirations and belief systems. Do all the students of Gryffindor turn out to be brave soldiers? If you asked the Hat, his response would be "Of course not! Whatever gave you that idea?" Rather, the Sorting Hat--and the Sorting in general--is meant to help foster and generate qualities that the students embody and aspire too.
Perhaps the most interesting part of the Sorting Hat's power is that it is capable of invalidating itself. The Founders of Hogwarts were aware of why they were dividing the students into houses: they felt that this way they could each nurture the traits that they valued most. In that way, the different houses would help the students become the best versions of themselves. However, they were not blind to the risks of such division. Therefore, they gifted the Sorting Hat with the authority to cease the Sorting at Hogwarts if it seemed as though the school was turning against itself. Should the Hat feel that sorting causes more harm than good, the houses would be abolished, and the school would resort to random dormitory assignments. Luckily, the Sorting Hat has never felt the school was anywhere close to that level of division; though, it did gently ponder it during the anti-Slytherin feelings after the War Against Voldemort. However, it quickly determined that things would get better and was correct.