The first church of Hagia Sophia was built on the same site in the 4th century by Constantine the Great and renovated by his son Constantinus II in 360 AD. It was a small wooden church in Constantinople. Unfortunately nothing remained from it because it was destroyed during a fire in 404 AD.
After the destruction, a second and larger Hagia Sophia was built at the same location in 415 AD by the emperor Theodosius II. This second church was also burned down during the Nika riots of 532 AD. Some of its columns, capitals, and the stairs can be seen today in the courtyard of the museum.
Finally, the third Hagia Sophia, the one that you can visit today, was built by emperor Justinian I between 532-537 AD over the remains of the previous basilica. The emperor spent almost all of his treasure, 10.000 people worked in its construction under the supervision of two architects; Anthemius of Tralles (modern day Aydin city) and Isidorus of Miletos. After completion, Justinian entered the church and he shouted "Solomon, I have outdone thee!", referring to King Solomon. The church became the glorious symbol of the Byzantine Empire and the largest church of Christendom in the world. For almost 1000 years the Hagia Sophia was the seat of the Orthodox Patriarch of Constantinople. Church councils and imperial ceremonies were held here.
The gigantic central dome over a rectangular plan was built using special bricks; 12 of them weighted as one regular. But it was still too heavy therefore this early dome collapsed during several earthquakes so a smaller one was built. In the days when there was no steel used in construction, large domes had to be supported by massive pillars and walls, thus the dome of Hagia Sophia was supported by four huge piers in order to take off its pressure on the side walls and distribute it to the ground. Fourty small windows around the dome and other windows of the church let enough light into the interior.
The interior walls of the church were decorated with gold mosaics, the floors with white marble, and column capitals with the monograms of Justinian and Theodora. Marbles and columns taken from the remains of earlier civilizations from all parts of the Empire were used as building material, these pieces came from Baalbek, from Pergamon, and from the Temple of Artemis as well.
The upper galleries were used by important people or for church councils during the Byzantine period, and lower part was used by common people. When the Hagia Sophia was a mosque, the galleries were reserved for the women during prayers, and lower floor was used by the men.
In 1204 the church was sacked by the Fourth Crusade, many precious relics were removed from the church and taken away. This act definitively divided the Greek Orthodox and Roman Catholic churches. Some of these relics can be seen today in the treasury of St. Mark's Basilica in Venice, Italy. Enrico Dandolo, the Doge of Venice who commanded Latin forces during the invasion of the city, is buried inside the church on the upper gallery.
On May 29th, 1453, the Ottomans conquered Constantinople and sultan Mehmet II ordered to convert the church into a mosque. Because he admired the art, the sultan didn't want these great mosaics to be destroyed so he plastered them over and the Ottomans made their own floral designs or geometrical patterns, as well as Coranic calligraphy on top of the plaster. In order to use it as a mosque, Mihrab and Minbar were added inside, a fountain for the ablution was placed in the courtyard, and minarets were built in different periods in the outer corners of the building. A Koranic school, soup kitchen, library, madrasa, the clock-winding house, and sultan's mausoleums (belonging to Selim II, Murat III, Mehmet III, Mustafa I and Ibrahim) are amongst the structures added by the Ottomans. Also, large buttresses were built by Turkish architect Sinan in the 16th century to support the walls holding up the dome and to save the building from the earthquakes. The sultan's loge was added in the 19th century during the restoration of the mosque by the Swiss origin Fossati brothers.
Aya Sofya remained a mosque for almost 500 years until 1935 when Atatürk converted it into a museum so everybody could come to visit this architectural masterpiece and admire both Christian and Muslim art. Prayer rugs were removed from the marble floor and experts came from all around the world to remove some of the plaster in order to uncover spectacular Byzantine mosaics. It was, and still is, an important task during the restorations bringing to light all the major Byzantine mosaics but also preserving the Islamic art and calligraphy to maintain a balance between both Christian and Islamic cultures.
The Hagia Sophia has a classical basilica plan measuring 74.67 x 69.80 meters (245 x 229 feet). The dome is not perfectly round having a diameter of 31.87 - 30.87 meters (104.5 - 101.3 feet), it's 55.60 meters (182.4 feet) high from the floor.