To symbolize their positions on their vessel.
In the original series and "Star Trek: Enterprise" blue shirts were to signify medical and science officers, yellow for command officers, and red were for operations officers.
After the original series the shirt colors were switched. Blue shirts continued to be medical and science officers, while red and yellow switched. Red for command officer and yellow for operations officers.
Also, NBC was owned by RCA who was first marketing color TV's. To help make TV's more popular, it was proposed that the crew wear "colorful" uniforms to show the difference between a black-and-white and color TV