St. Andrew was martyred by crucifixion at Archae, by being tied to a cross similar to the one Jesus was crucified on. Such a cross is known as crux decussata (a cross in the shape of a Roman numeral X), and thus the symbol of the white cross being used on the flag. Although the iconography of Andrew on the cross was common in art, and the cross itself was used on seals, coins as far back as the 13th Century, the adoption of the flag in Scotland only dates to the 1500's.
One of the most interesting stories surrounding the image comes from the days before there was such a country as Scotland. In the year 832, Óengus II, leading an army of Picts and Scots against the Angles of King Aethelred is said to have given a prayer to St. Andrew, stating that if they were given victory, he would appoint him as patron saint of "Scotland". Before the battle he looked to the sky and saw the white clouds making a cross in the blue. Believing this to be a sign of support from the saint the armies of Óengus were spurred to victory, despite smaller numbers than Aethelred.
In 1606, the white cross on blue background was incorporated into the Union Flag, newly created to represent the Union of the Crowns in 1603, when James VI of Scotland, became also James I of England, uniting both his inherited kingdoms into one.
Other uses: An ancient superstition in northern England and Scotland states that a St. Andrew's Cross carved or placed by a fire place will prevent witches from flying down the chimney. As well as Scotland, Andrew is the patron saint of other countries, and the sign of the white cross on blue can be seen in many of these places. The flag of Tenerife, the "St. Andrew's Flag" of the Netherlands, In certain Russian Navy flags you can find an inverted, blue cross on white, version of the St. Andrew's Cross.