The Flag of Scotland is the Saltire: the white diagonal cross of Scotland's patron saint, St Andrew, on a blue field. It is one of the oldest flags in the world, dating back, according to the version of the story you believe, to 832 or further, perhaps to 761.
According to the most popular version of the legend, a joint force of Picts and Scots under King Angus (or Óengus) of The Picts met a Northumbrian army of Angles under King Athelstan at a location near Athelstaneford, four miles north east of Haddington, in East Lothian, in 832. The Picts and Scots were heavily outnumbered, and the night before the battle, King Angus prayed for victory. It is said that during the night St Andrew appeared to Angus in a dream and promised him the victory he had prayed for.
The following morning the two armies formed up for battle. As they did so, a strange cloud formation appeared, forming a broad diagonal white cross against the background of bright blue sky. The Picts and Scots believed this to be an omen: and so did the Angles. The battle that followed was an improbable victory for the outnumbered Picts and Scots. And the Saltire has been the Flag of Scotland ever since.
A nice story, but one that has a number of problems. Although the site of the battle is close to the village now called Athelstaneford, Athelstan wasn't actually born until 895. And he wasn't King of the Angles. He was the first King of England. And he didn't lose in battle to the Scots and Picts. On the contrary. At the Battle of Brunanburh, in 937, he led English forces to victory over joint Scottish and Viking armies under King Constantine II and King Olaf III Guthfrithson. This was one of the most significant battles in British history, defining forever the existence and approximate boundaries of England.
This makes the legend of the foundation of the Saltire begins to look a little like spin-doctoring in the centuries that followed, in an effort (very successfully) to airbrush the disastrous Battle of Brunanburh out of history in favour of a story that showed Athelstan in a much worse light: and the Scots more favourably.
Another version of the same story comes from a Latin history of Scotland, The Scotichronicon, written in the 1440s by Walter Bower. This has the key player the Pictish King Unust, who was fighting the Northumbrians: the story is otherwise the same. This would date the origin of the Saltire back as far as 761.
One of the oddities of the Saltire is that the shade of blue used for the background has never been defined, varying between the traditional sky blue at one extreme and a dark navy blue at the other. Different versions are shown on the left. In 2003 the Scottish Parliament proposed that a shade of Pantone 300 (as in the header image) be used, and this is becoming the standard. The flag is normally made in the proportions of 3:2 or 5:3, and the width of the stripes in the cross is usually one fifth of the height of the flag.
The Saltire became part of the Union Flag following the Union of the Crowns of England and Scotland in 1603. The image below shows how the flags of England and Scotland were combined to form the Union Flag of 1606: and then with the flag of Ireland to form the 1801 Union Flag. An image on this page shows one of several designs not adopted in 1606.