The area of present-day Datong was close to the Beidi ("Northern Barbarian") state of Dai, which was conquered by the Zhao clan of Jin in 457 BC. It was a frontier land between the agricultural Chinese and Beidi and the nomads of the Eurasian steppe (known to the Chinese as the Hu or Donghu). The area was well known for its trade in horses.
Pingcheng County formed part of the Qin commandery of Yanmen. It continued under the Han, who founded a site within present-day Datong in 200 BC following their victory against the Xiongnu nomads at the Battle of Baideng. Located near a pass to Inner Mongolia along the Great Wall, Pingcheng blossomed during the following period and became a stop-off point for camel caravans moving from China into Mongolia and beyond. It was sacked at the end of the Eastern Han. Pingcheng became the capital of Northern Wei from AD 398–494. The Yungang Grottoes were constructed during the later part of this period (460–494). During the mid to late 520s, Pingcheng was the seat of Northern Wei's Dai Commandery.
The city was renamed Datong in 1048. It was the Xijing ("Western Capital") of the Jurchen Jin dynasty prior to being sacked by the Mongols. It was sacked again at the end of the Ming in 1649, but promptly rebuilt in 1652.