Mountains can be formed more than one way, but the commonest is by uplift. The surface of the Earth is composed of a number of separate areas called tectonic plates, which float on the underlying mantle. Overall the tectonic plates fit together like puzzle pieces, but as they move sometimes two of them will push toward each other, and they will push each other upward and form a mountain range. Some mountains are coastal, but others (for instance the Himalayas and the Rockies) are far inland; it all depends on where the edges of the plates are.
As for the second part of your question, keep in mind that the mountains came first, and the cities were settled later. People tend to settle where there are resources that they need, such as a steady supply of drinking water, arable land for producing food, and materials for building things. Since mountains tend to be an impediment to travel and transportation of materials, and also tend to have areas of harsher climate, most cities have grown up in flatter locations that were easier for people to live in.