For centuries a major administrative, shipping, and trading town, Shanghai grew in importance in the 19th century due to European recognition of its favorable port location and economic potential. The city was one of five opened to foreign trade following the British victory over China in the First Opium War while the subsequent 1842 Treaty of Nanking and 1844 Treaty of Whampoa allowed the establishment of the Shanghai International Settlement and the French Concession. The city then flourished as a center of commerce between east and west, and became the undisputed financial hub of the Asia Pacific in the 1930s. However, with the Communist Party takeover of the mainland in 1949, trade was reoriented to focus on socialist countries, and the city's global influence declined. In the 1990s, the economic reforms introduced by Deng Xiaoping resulted in an intense re-development of the city, aiding the return of finance and foreign investment to the city.

During the Song dynasty (960–1279) Shanghai was upgraded in status from a village to a market town in 1074, and in 1172 a second sea wall was built to stabilize the ocean coastline, supplementing an earlier dyke. From the Yuan dynasty in 1292 until Shanghai officially became a municipality in 1927, central Shanghai was administered as a county under Songjiang Prefecture, whose seat was at the present-day Songjiang District.

Two important events helped promote Shanghai's development in the Ming dynasty. A city wall was built for the first time in 1554 to protect the town from raids by Japanese pirates. It measured high and in circumference. During the Wanli reign (1573–1620), Shanghai received an important psychological boost from the erection of a City God Temple in 1602. This honour was usually reserved for places with the status of a city, such as a prefectural capital not normally given to a mere county town, as Shanghai was. It probably reflected the town's economic importance, as opposed to its low political status.[3]

During the Qing dynasty, Shanghai became one of the most important sea ports in the Yangtze Delta region as a result of two important central government policy changes: In 1684, the Kangxi Emperor reversed the Ming dynasty prohibition on oceangoing vessels – a ban that had been in force since 1525; and in 1732 the Yongzheng Emperor moved the customs office for Jiangsu province (; see Customs House, Shanghai) from the prefectural capital of Songjiang to Shanghai, and gave Shanghai exclusive control over customs collections for Jiangsu's foreign trade. As a result of these two critical decisions, by 1735 Shanghai had become the major trade port for all of the lower Yangtze region, despite still being at the lowest administrative level in the political hierarchy.

Citizens of many countries and all continents came to Shanghai to live and work during the ensuing decades; those who stayed for long periods – some for generations – called themselves "Shanghailanders". In the 1920s and 1930s, almost 20,000 White Russians and Russian Jews fled the newly established Soviet Union and took up residence in Shanghai. These Shanghai Russians constituted the second-largest foreign community. By 1932, Shanghai had become the world's fifth largest city and home to 70,000 foreigners. In the 1930s, some 30,000 Jewish refugees from Europe arrived in the city.

The Sino-Japanese War concluded with the Treaty of Shimonoseki, which elevated Japan to become another foreign power in Shanghai. Japan built the first factories in Shanghai, which were soon copied by other foreign powers. Shanghai was then the most important financial center in the Far East. All this international activity gave Shanghai the nickname "the Great Athens of China"

Under the Republic of China (1912–49), Shanghai's political status was raised to that of a municipality on 14 July 1927. Although the territory of the foreign concessions was excluded from their control, this new Chinese municipality still covered an area of , including the modern-day districts of Baoshan, Yangpu, Zhabei, Nanshi, and Pudong. Headed by a Chinese mayor and municipal council, the new city government's first task was to create a new city center in Jiangwan town of Yangpu district, outside the boundaries of the foreign concessions. The "Greater Shanghai Plan" included a public museum, library, sports stadium, and city hall, which were partially constructed when the plan was interrupted by the Japanese invasion.

On 28 January 1932, Japanese forces struck and the Chinese resisted, fighting to a standstill; a ceasefire was brokered in May. The Battle of Shanghai in 1937 resulted in the occupation of the Chinese administered parts of Shanghai outside of the International Settlement and the French Concession. The foreign concessions were occupied by the Japanese on 8 December 1941 and remained occupied until Japan's surrender in 1945, during which time war crimes were committed.

On 27 May 1949, the People's Liberation Army took control of Shanghai. Under the new People's Republic of China (PRC), Shanghai was one of only three municipalities not merged into neighboring provinces over the next decade (the others being Beijing and Tianjin). Shanghai underwent a series of changes in the boundaries of its subdivisions, especially in the next decade. After 1949, most foreign firms moved their offices from Shanghai to Hong Kong, as part of a foreign divestment due to the Communist victory.

During the 1950s and 1960s, Shanghai became an industrial center and center for radical leftism; the leftist Jiang Qing and her three cohorts, together the Gang of Four, were based in the city. Yet, even during the most tumultuous times of the Cultural Revolution, Shanghai was able to maintain high economic productivity and relative social stability. During most of the history of the PRC, Shanghai has been a comparatively heavy contributor of tax revenue to the central government, with Shanghai in 1983 contributing more in tax revenue to the central government than Shanghai had received in investment in the prior 33 years combined. This came at the cost of severely crippling Shanghai's infrastructural and capital development. Its importance to the fiscal well-being of the central government also denied it economic liberalizations begun in 1978. Shanghai was finally permitted to initiate economic reforms in 1991, starting the massive development still seen today and the birth of Lujiazui in Pudong.