The origins of the flag date back to 1603, when James VI of Scotland inherited the English and Irish thrones (as James I), thereby uniting the crowns of England, Scotland and Ireland in a personal union (which remained separate states). On 12 April 1606, a new flag to represent this regal union between England and Scotland was specified in a royal decree, according to which the flag of England (a red cross on a white background, known as St George's Cross), and the flag of Scotland (a white saltire on a blue background, known as the Saltire or St Andrew's Cross), would be joined together, forming the flag of Great Britain and first union flag.
The current design dates from a Royal Proclamation following the Union of Great Britain and Ireland in 1801. The flag combines aspects of three older national flags: the red cross of St George for England and Wales, the white saltire of St Andrew for Scotland (which two were united in the first Union Flag), and the red saltire of Saint Patrick's Flag to represent Ireland.
Info about the Scottish flag: