“Which came first, the phoenix or the flame?”
"…I think the answer is that a circle has no beginning."
THE FOUNDERS OF HOGWARTS - Rowena Ravenclaw
“We’ll teach those whose intelligence is surest.”
It’s a combination of the two. Ultimately, it’s your choice that trumps all (or thematically, it should be - “it is our choices, not our abilities”) but we see enough canonical Sortings to see that the Hat takes both into consideration. The Hat won’t put you somewhere you don’t want to be (it may try to argue you into a House, but it won’t put you there outright), but it also won’t put you somewhere you’re completely unsuited to. It’s a combination of choice, values, and abilities: it’s a complex interplay of the three, with conscious choice ultimately trumping the other two. (Why, then, would you have a Sorting? So the Hat can point out/discuss each House with the students, and so the students don’t have to do what their parents/siblings want - a public Sorting that would force students to openly choose would put someone like Sirius Black in a very hard position if he didn’t want to get kicked out of the house at age 11. And for students who are undecided/have no strong opinions, the Hat can put them where their abilities are strongest/where they’ll find like-minded people who are likely to be predisposed to friendship.)*
(Gilderoy Lockhart’s Sorting: based on what he values alone, he’d be in Gryffindor, but per JKR the Sorting was narrowly between Slyth and Claw. Peter Pettigrew: you can make a case, swimming strongly against canon and Pottermore, that he exhibited more Gryffindor traits than any other House, but I tend to think that JKR would say that values won out there. Neville Longbottom obviously had and valued Gryff traits, but his asking for Puff was ignored because the Hat knew the only reason he was asking was that he didn’t feel good enough for Gryffindor - but it still had to persuade him to agree to Gryffindor before Sorting him there.)
*granted, for some things this is a terrible idea - notably the canonical blood-purity aspect of Slytherin, which ends up being “let’s take all the students most likely to be prejudiced and put them in a House where they can all reinforce each other’s prejudice!” - but from an in-universe historical standpoint, the Founders were thinking more about “I’d like to teach kids who are like me/who are likely to agree with my worldview” and not “how are we going to produce well-rounded students?”
Pottermore ♦ Harry Potter and The Goblet of Fire 
I’m not trying to be stupid or anything, but I’m a bit confused by the geography of Hogwarts. Particularly of Gryffindor’s dorm tower.
For example, when Harry and Ron go into their dorm, they are sharing it only with Dean, Seamus, Neville. BUT…there are Gryffindor’s from all different years in the dorm. Why is it, when they go up the stairs they go to their room, yet can still go into other year’s dorm? Like, Fred and George don’t sleep with Harry and Ron right? But they are apart of the same dorm and go up the same stairs…what?
Each year has its own room - the dormitories are divided sides for male and female, and within those dormitories there are individual rooms for each year (so that on the boys’ side, year 1 shares a room, year 2 shares a room, etc. I assume that each grouping keeps the same room for the 7-year-stay at Hogwarts and doesn’t rotate into a room specifically designated for ‘year 1 gryffindor boys,’ ‘year 2 gryffindor boys,’ etc., but that’s never explicitly stated. (Given that the number of Gryff boys would vary year by year, it would make more sense to assign one dorm room for the 7-year-stay, but I’m sure you could use magic to move beds and stuff without much problem.)
Hopefully that explanation makes sense. (Also, I assume that Gryffindor Tower has at least one communal bathroom for all boys and one communal bathroom for all girls with showers/toilets/etc., much like a college dorm would, but that’s speculative. It does make sense, though.)
I finished all of the Hogwarts House mascots!
You can now purchase the entire set or the individual art prints in my Etsy shop. Enjoy!
Or yet in wise old Ravenclaw
if you’ve a ready mind.