The current building was originally constructed as a church between 532 and 537 on the orders of the Byzantine Emperor Justinian I and was the third Church of the Holy Wisdom to occupy the site, the prior one having been destroyed by rioters in the Nika Revolt. It was designed by the Greek geometers Isidore of Miletus and Anthemius of Tralles. The church was dedicated to the Wisdom of God, the Logos, the second person of the Trinity, its patronal feast taking place on 25 December, the commemoration of the birth of the incarnation of the Logos in Christ. Although sometimes referred to as Sancta Sophia (as though it were named after Sophia the Martyr), sophia being the phonetic spelling in Latin of the Greek word for wisdom, its full name in Greek is Ναός της Αγίας του Θεού Σοφίας, Naos tēs Hagias tou Theou Sophias, "Shrine of the Holy Wisdom of God". The church contained a large collection of relics and featured, among other things, a 15-metre (49 ft) silver iconostasis. The focal point of the Eastern Orthodox Church for nearly one thousand years, the building witnessed the excommunication of Patriarch Michael I Cerularius on the part of Humbert of Silva Candida, the papal envoy of Pope Leo IX in 1054, an act that is commonly considered the start of the East–West Schism.
In 1453, Constantinople was conquered by the Ottoman Empire under Mehmed the Conqueror, who ordered this main church of Orthodox Christianity converted into a mosque. Although some parts of the city of Constantinople were falling into disrepair, the cathedral was maintained with an amount of money set aside for this purpose. Nevertheless, the Christian cathedral made a strong impression on the new Ottoman rulers and they decided to convert it into a mosque. The bells, altar, iconostasis, and other relics were destroyed and the mosaics depicting Jesus, his Mother Mary, Christian saints, and angels were also destroyed or plastered over. Islamic features—such as the mihrab, minbar, and four minarets—were added. It remained a mosque until 1931 when it was closed to the public for four years. It was re-opened in 1935 as a museum by the Republic of Turkey. Hagia Sophia was, as of 2014, the second-most visited museum in Turkey, attracting almost 3.3 million visitors annually. According to data released by the Turkish Culture and Tourism Ministry, Hagia Sophia was Turkey's most visited tourist attraction in 2015.
From its initial conversion until the construction of the nearby Sultan Ahmed Mosque (Blue Mosque of Istanbul) in 1616, it was the principal mosque of Istanbul. The Byzantine architecture of the Hagia Sophia served as inspiration for many other Ottoman mosques, such as the aforementioned mosque, the Şehzade Mosque, the Süleymaniye Mosque, the Rüstem Pasha Mosque and the Kılıç Ali Pasha Complex.
The first church on the site was known as the Μεγάλη Ἐκκλησία (Megálē Ekklēsíā, "Great Church"), or in Latin Magna Ecclesia, because of its larger dimensions in comparison to the contemporary churches in the City. Inaugurated on 15 February 360 (during the reign of Constantius II) by the Arian bishop Eudoxius of Antioch, it was built next to the area where the imperial palace was being developed. The nearby Hagia Eirene ("Holy Peace") church was completed earlier and served as cathedral until the Great Church was completed. Both churches acted together as the principal churches of the Byzantine Empire.
Writing in 440, Socrates of Constantinople claimed that the church was built by Constantius II, who was working on it in 346. A tradition which is not older than the 7th or 8th century, reports that the edifice was built by Constantine the Great. Zonaras reconciles the two opinions, writing that Constantius had repaired the edifice consecrated by Eusebius of Nicomedia, after it had collapsed. Since Eusebius was bishop of Constantinople from 339 to 341, and Constantine died in 337, it seems possible that the first church was erected by the latter. The edifice was built as a traditional Latin colonnaded basilica with galleries and a wooden roof. It was preceded by an atrium. It was claimed to be one of the world's most outstanding monuments at the time.