It is as Meer states, that they distinguish between opponents. However, not all styles use red and blue. In Taekwondo, it is required to wear the color of your belt (for poomsae competition), while in JKA, Red and White are worn, while in Olympic Karate, Red and Blue are worn. Incidentally, in Taekwondo, while natural belt colors are worn for poomsae divisions, in sparring, Red and Blue are worn (Chung and Hong). Usually, though, without e-scoring, the color of the chest protector is the color of the competitor, which is why white hogus are white with red on one side and blue on the other. They are switchable for just this purpose.
As a referee, I can tell you that is is much better to have differing colors, because it makes it easy to point out which competitor gets the point or warning. The rule books don't go into the whys and wherefores, but, from experience, I can tell you that the colors really do make a difference.
As an example, a common judging format is to have a judge sit with two pieces of paper, each divided down the middle. Left side is for competitor #1, while right side is competitor #2. In this type of scenario, it is easy for sparring opponents to cross and re-cross, and so, "competitor #1", or "left competitor", etc can easily be confused.
As to why the specific colors were chosen, I cannot say. As to why different colors are chosen, I can definitely say it is to reduce chances for error in scoring a competitor.
USA Olympics - Red/Blue
JKA - Red/White