Gaelic was first brought to Scotland around the 4th century AD by Irish settlers, and at some point thoroughly replaced Pictish in the Islands and Highlands. We can’t be entirely certain how, why, or when Gaelic replaced Pictish, but we do know that Pictish was spoken in Scotland during the time the Romans occupied Britain. The Romans left around 400AD.
Scottish Gaelic remained quite similar to Irish for a very long time. There were a lot of trading and other relations between the two countries, and Gàidhlig-speaking Christians used an Irish Bible until a Gàidhlig-language one was published in 1801.
Gaelic fell out of use in the royal court and government of Scotland around 1070, when “Inglis”, or a low German dialect spoken by the Anglo-Saxons, was introduced by Malcolm II (also known as Ceann Mòr or Canmore, which means Big Head) and his wife, the English princess Margaret. Malcolm Canmore had fled to England during the reign of Macbeth, but then overthrew him in 1057 with the help of the Anglo-Saxons (the English). Those same Anglo-Saxons later fled to Scotland during the Norman conquest of England. They considered Gaelic to be backwards and uncivilised (no doubt entirely because they couldn’t understand it) and refused to learn it, causing “Inglis” (known today as “Scots”) to become the main language of the court and government. It quickly became the common language throughout the lowlands.
However, Gaelic continued to be in common use in the Highlands and Islands for many hundreds of years, until the Highland Clearances during the 18th and 19th centuries forced many Gaelic-speakers to relocate to Canada, and later to New Zealand. After the Clearances, Gaelic was actively repressed by the English. In 1872, the Education Scotland Act made no mention of Gaelic, which caused several generations of children to be forbidden to speak Gaelic in school, and were even at times beaten for speaking it! Many children who endured this are still alive today.The numbers of Gaelic-speakers in Scotland fell quite rapidly from around 231 600 monolingual Gaelic-speakers in 1881 to only around 43 700 in 1891. As of the 1981 census, there were no monolingual Gaelic-speakers left in Scotland, and only around 82 500 bilingual English and Gaelic speakers, as compared to 210 700 bilingual individuals on the 1891 census. Almost all of the native Gaelic-speakers left in Scotland live in the Outer Hebrides, where around 65% of the population consider it their first language, and the Inner Hebrides, where around 35% of the population do.
Today, Gaelic is being encouraged in Scotland, and there are a number of organisations devoted to preserving the language. A number of schools also teach solely through the medium of Gaelic, as well as one university, although there are several other universities which offer some courses through Gaelic.