Yellow is for specialties related to "ship functions", including engineering, security/tactical, and the Science Officer. Blue is for other sciences not related to ship functions, including research and medicine (including psychiatric medicine, which is the role of the ship's Counsellor). Red is for generalists, including command ranks and trainees and most or all ensigns who haven't moved up to a blue or yellow specialty yet.
It wasn't that way in Kirk's generation; the famous "red shirts" were usually "security" (and good at it; their sacrifices kept the higher officers safe!), the Science Officer wore blue, the Captain wore yellow... there might not have been any particular duty-related pattern or plan.