As was pointed out by others, colours in national flags are derived from older coat of arms or other symbols derived from medieval heraldry. That one itself become more or less formalized in the early 13th century, from a habit taken (especially during the Crusades) to hoist highly distinguishable colour patterns so as to promote warrior recognition on a dusty battlefield. Thus a relatively small set of primary colours, including red, which is very visible in a countryside landscape (the human eye is most sensitive to green, not red, but Nature is full of green so a green flag does not stand out the way a red flag does).
Among heraldry "rules" is the one about tinctures that says that you should put only a metal on a colour, or vice-versa, but not metal on metal or colour on colour. "Metal" is or (yellow) or argent (white); other tinctures are "colours". This rule is one of maximum visibility: to make details visible, they must be painted with tinctures that don't blend. These rules can still be seen in action on car license plates; for instance, old French plates were white-on-black, while recent ones are black-on-white or black-on-yellow; Belgian licence plates are red-on-white; and so on. A consequence is that most coat of arms contained some white or yellow (hard to avoid, with the rule explained above), which now shows on national flags.
It is noteworthy that flag evolution has not been straight from medieval banners to national flags; maritime flags played an important part, and visibility (in a nautical context) was the whole point of such flags.
While the above talks about plausible reasons why red would be most favoured (mainly because it is highly visible), this may be all purely coincidental. Worldwide statistics on national flags, by definition, work over nations, and a lot of nations were created in recent history. In particular, most of Africa (now 54 UN members) became independent countries in the last 70 years, and "invented" their flags at that time. Most of them reused some or all of the colours from the Pan-African flag, including the red. Warfare technology being what it is now, flag visibility can be said to no longer have any practical consequence; symbolism is a much stronger force in national flag colour selection. These African flags alone account for more than a quarter of current national flags, so such coincidental effects cannot be dismissed easily.
A similar effect may be observed in formerly communist states, who tended to use the "communist red" in their flags (e.g. China, Vietnam).
Yet another case is the Red Ensign that "pollinated" a lot of ulterior national flags thanks to the ubiquitousness of the British Empire in the late 1800s. For instance, Canada's flag red part is a deliberate reference to England (while the white part is explicitly a reference to the French pre-Revolution inheritance).