As a sacred place
In Europe, Christian churches were usually built on a holy spot, generally where a miracle or martyrdom had taken place or where a holy person was buried. Examples are St. Peter's Basilica in Rome and St. Albans Cathedral in England, which commemorate the martyrdom of Saint Peter (the first Pope) and Saint Alban (the first Christian martyr in Britain), respectively. The place, and therefore the church built there, was considered to have been sanctified (made holy) by what happened there. In modern times, the Roman Catholic Church has continued this practice by placing in the altar of each church, when it is consecrated for use, a box (= sepulcrum) containing relics of a saint. The relics box is removed when the church is taken out of use as a church. In the Eastern Orthodox Church, the antimension on the altar serves a similar function. It is a cloth icon of Christ's body taken down from the cross, and typically has the relics of a saint sewn into it. In addition, it is signed by the parish's bishop, and represents his authorization and blessing for the Eucharist to be celebrated on that altar.
The area around the altar was also considered holy because of the physical presence of God in the Eucharist (= communion bread, which Catholics considered to have been 'transubstantiated" into the actual body of Jesus), both during the Mass and in the tabernacle on the altar the rest of the time. So that people could tell when Jesus was there (in the tabernacle), the "sanctuary lamp" would be lit, indicating that anyone approaching the altar should genuflect (= bow by bending the knee and inclining the head), to show respect for Him. In most Eastern Orthodox churches, the sanctuary is separated from the nave (where the people pray) by an iconostasis, literally a wall of icons, with three doors in it. In many Roman Catholic churches, altar rails mark the edge of the sanctuary.
The area around the altar came to be called the "sanctuary," and that terminology does not apply to Christian churches alone: King Solomon's temple, built in about 950 B.C., had a sanctuary ("Holy of Holies") where the tabernacle ("Ark of the Covenant") was, and the term applies to the corresponding part of any house of worship.
As a right of asylum
Many ancient peoples, including the Egyptians, the Greeks, and the Hebrews, recognized a religious right of asylum, protecting criminals (or those accused of crime) from legal action to some extent. This principle was adopted by the early Christian church, and various rules developed for what the person had to do to qualify for protection and just how much protection it was.
In England, King Ethelbert made the first laws regulating sanctuary in about 600 A.D. By Norman times, there had come to be two kinds of sanctuary: All churches had the lower-level kind, but only the churches the king licensed had the broader version. There were at least twenty-two churches with charters for that kind of sanctuary, including Battle Abbey, Beverley, Colchester, Durham, Hexham, Norwich, Ripon, Wells, Winchester Cathedral, Westminster Abbey, and York Minster.
Sometimes the criminal had to get to the church itself, to be protected, and might have to ring a certain bell there, or hold a certain ring or door-knocker, or sit on a certain chair ("frith-stool"), and some of these items survive at various churches. In other places, there was an area around the church, sometimes extending as much as one mile, and there would be stone "sanctuary crosses" marking the boundary of the area; some of those still exist today, too.
As it came to be codified in England, a felon who claimed the right of sanctuary could remain in the sanctuary area for thirty to forty days and then had to leave England ("abjure the realm") by the shortest route and never return without the king's permission. Anyone who did come back could be executed by the law and/or excommunicated by the Church.
Relating to political asylum
During the Wars of the Roses, when the Yorkists or Lancastrians would suddenly get the upper hand by winning a battle, some adherents of the losing side might find themselves surrounded by adherents of the other side and not able to get back to their own side, so they would rush to sanctuary at the nearest church until it was safe to come out. A prime example is Queen Elizabeth Woodville, consort of Edward IV of England:
In 1470, when the Lancastrians briefly restored Henry VI to the throne, Edward's queen was living in London with several young daughters. She moved with them into Westminster for sanctuary, living there in royal comfort until Edward was restored to the throne in 1471 and giving birth to their first son Edward during that time. When King Edward died in 1483, Elizabeth (who was highly unpopular with even the Yorkists and probably did need protection) took her five daughters and youngest son (Richard, Duke of York; Prince Edward had his own household by then) and again moved into sanctuary at Westminster. To be sure she had all the comforts of home, she brought so much furniture and so many chests that the workmen had to knock holes in some of the walls to get everything in fast enough to suit her.