Here, let me take a whack at explaining it.
On a starship, everyone has a different job. Each person's job can be distinguished by what colour uniform they wear. The uniform's colour is, more or less, unrelated to rank (except in the case of Captain and First Officer. The Captain and First Officer will always wear Red, but others can wear Red as well).
If you work as a security officer, an engineer, or some sort of technical skill (operating the transporter, for example), you will wear YELLOW.
If you work as a doctor, nurse, or some sort of scientist, you will wear BLUE.
If you pilot the ship, command the ship (or help command as the First Officer), or do other general tasks on the ship NOT RELATED to the Blue or Yellow colours, you will wear RED.
If you are a new officer, like an Ensign, you can choose whatever job you want, meaning you can choose to wear Red, Blue, or Yellow (or, more accurately, you begin to choose your career path in Starfleet based on what you study at Starfleet Academy, prior to graduation/being commissioned to serve as an officer on a starship. For example, you can't just decide to be a Starfleet Doctor when you're on the ship -- you must first go to Starfleet Medical). As you progress in rank, you can switch jobs whenever you want. That is why anyone who is not the Captain or the First Officer can be whatever rank in whatever colour uniform.
So, in summary, the colour of the uniform determines what your job is on the ship. The individual's rank determines a chain of command, irregardless of colour. That being said, certain specialists will hold a higher rank on the ship, but will not be put in positions where they will have to command the ship (such as the Chief Medical Officer or the Chief Engineer, since they are busy doing other important jobs and do not have experience commanding a starship even though they may hold the rank of Lt. Commander, for example).
O'Brien, Worf, and Geordi all started out as young officers that did not work in Engineering, Security, etc. However, as their careers progressed, they switched jobs to become Transporter Chief (O'Brien), Security Chief (Worf), and Chief Engineer (Geordi). They switched from wearing RED to YELLOW, but also progressed in rank. They were not demoted, as they are the senior ranking officer in their particular job (they're like the boss of Engineering or Security). They are still required to take orders from anyone ranked above them, but since their jobs are separated, you will not often see a Lt. Commander that works in Security ordering around a Lieutenant that works in Engineering, even though they could if they wanted to.
ex: Tuvok was a Lt. Commander in rank on Voyager, but his job was Chief of Security. He could theoretically order around B'Elanna, since she was only a Lieutenant, but he doesn't because she knows more about Engineering.
OK, so, if you are still confused, we'll start over.
On a Starship, it is useful to be able to quickly tell what a person's job is on the ship. In the Federation, this is done with a three colour code structure. The colours are as follows:
Red: Command, pilots, nonspecific
Blue: Science, Medical
Yellow: Engineering, Security, Operations (technical or computer work)
The only ranks that are required to wear RED are the Captain and the First Officer. Everyone else that is lower in rank (Lt. Commander and below), will be in whatever colour that their job requires, despite their rank. The rank does not determine the colour (unless you are a Captain or a Commander), but it does determine the chain of command. So, if a BLUE officer outranks a RED officer, the RED officer has to follow the BLUE officer's orders.
Thus, a young Ensign can choose to wear RED. This does not mean they are in command of a starship; rather, they're training/learning to one day hold a position of executive power, such as Captain or First Officer. They are outranked by just about everyone on the ship, despite wearing RED. For example, Worf started out wearing RED, probably because he wanted to one day command a ship. As he rose in rank, he chose to work in Security, and thus wore YELLOW. As he continued still to rise in rank, he switched back to wearing RED, probably because he was hoping to be a First Officer or Captain soon.
Two points of confusion:
1. Anyone wearing RED is said to be in the Command Department, as in their 'job' is to command. The term "Command" should be thought of more as what they're training to do. A Lieutenant wearing RED is receiving training in making executive decisions and commanding a starship (in general), instead of training to work as an engineer (for example). This young, RED officer is specifically looking to command a ship one day (instead of being a great engineer, for example), even though they're not actually Commanding anything as a Lieutenant. They are, essentially, learning how to command a starship. They're still subject to the orders of any officer that outranks them, even if that officer wears YELLOW or BLUE.
2. Some officers will be called Commander even though they are not wearing RED. For example, Data is referred to as "Commander Data" even though he wears YELLOW. Why is Data not the First Officer? Because Data's rank is actually Lt. Commander, but in the military, it is customary to refer to a "Lt. Commander" as "Commander" for short, since it would be degrading to refer to them as "Lieutenant." Therefore, Lt. Commander Data wears YELLOW, since he is not a Commander by rank nor is he First Officer or Captain, but may still be referred to as "Commander."
Now, to make things even more confusing: In the Star Trek series set before TNG (such as The Original Series and Enterprise), the colours of YELLOW and RED were switched. Thus, before the 24th Century (the time period of TNG, DS9, and VOY), the colour structure was as follows:
Yellow: Command, pilot, miscellaneous
Red: Engineering, security, operations
Blue: Science, medical
While Enterprise was made AFTER Voyager, it followed this old colour scheme since it was set in the time period before TOS.
The Final Word:
An individual's rank determines whom they have power over. An individual's colour determines their job on the ship.