Also, some places physically cannot grow outwards anymore, either because other cities are in the way or because of geographical elements like mountains being in the way. Barcelona is one such city, with one of the highest population density per km2. Only way is to build taller buildings, but no one wants to live in a city where you can't see the sky (ya ya, except Hong Kong and other places, don't ask me how they do it).
Yes, I know, I was there again very recently and it's still mostly as nice as I remembered, lovely city all around and fascinating buildings, infrastructure and overall city :) It's quite different than other metropolitan cities, and I didn't mean it as a jab or anything, was just trying to "paint a picture", no offense meant :)