Also don't forget that in cities, especially walled ones, it was often easier to build UP than it was to build out.

The idea of the modern 10+ story apartment building isn't actually modern. The poorest housing would of course be at the top. Medieval cities packed a lot of people into a small space.