In TNG, Riker is the first officer and has the rank of Commander. Lt. Commander Data is the 2nd officer, yet Crusher outranks Data. Why is it that he's the 2nd officer and she isn't?
Where does she fit into the command hierarchy?
Beverly Crusher holds the rank of Commander as well as being the ship's Chief Medical Officer. She periodically acts as OOD (Officer of the Deck) on the night shift but remains outside the normal command hierarchy at other times.
In TNG : Gambit we see that that Enterprise' standard Bridge Command structure (based on who takes over as each senior officer leaves) goes Picard > Riker > Data > Worf > LaForge > Troi.
The positions of OOD, 1st Officer and 2nd Officer are not based on rank but are at the discretion of the Ship's Captain. In TNG : Best of Both Worlds (Part II), Captain Riker makes Lt. Commander Shelby his 1st Officer over Data, even though he acknowledges that this is an unusual act as it also results in Worf not moving up into the 2nd Officer position.
RIKER : Lieutenant Worf, everyone at this table shares my respect for your service to this ship. But right now, I need your experience at tactical... Mister Data, I realize that your very nature omits ambition. Nevertheless, I want you to know I thought seriously about you as First Officer as well...
Being the first or 2nd officer is not a rank - it's an occupation, distinguishing ships chain of command. So is being chief medical officer, like Beverly is. Military ranks are different, they work more or less independently, that is - to get an occupation you need required minimum rank and that's about it.
According to Memory Alpha referencing episode Thine Own Self Beverly Crusher is a Commander as opposed to Datas Lt. Commander and so she outranks him even though on the ships hierarchy they (kind of) do not fall within the same command chain.
The hierarchy of ship operation is not tightly bound to any particular rank. Various positions have a minimum rank, but having the rank does not qualify an individual for that position. As a sweeping generalization the positions usually match the rank so there are no issues of a lower position having a higher rank and thus disagreeing about the orders (see #3 below).
Picard outranks Crusher, but he will never be the ship's doctor.
Similarly, Crusher is not qualified to run the helm even though 'Ensign' is the minimum rank.
Picard IS qualified to run the helm but if Picard has the helm, Data has the bridge, and the Romulans decide to visit, Worf would be in a very difficult situation if Data and Picard gave conflicting orders. Data has command of the bridge but Picard outranks Data.
Most people would not place Worf in Troi's job, although I once read this about Klingon counsellors:
I understand you have received news of your father's passing. Come - we will celebrate his death with wine and song!
There are a lot of really good answers already, focusing on the rank structure, how Crusher fits into it and highlighting that the role of Chief Medical Officer (CMO) is separate from rank. But not really covering why the CMO is special, so lets talk about that and clear up the differences between Crushers many Titles, Roles, Qualifications and Positions:
Qualifications and Professional Titles
Professional Title: Doctor
Doctor Crusher holds several medical qualifications, including a Doctorate of Medicine, granting her the professional title "Doctor" as well as the suffix "MD" refering to medical doctor. Of course someone with a Doctorate of Theoretical Physics (like Dr. Leah Brahms) would be a Doctor, but not a MD.
Starfleet Rank: Commander
Doctor Crusher also hold the military rank and title of Commander, reflecting a long 14 year career within Starfleet. On the Enterprise D this gives her joint second highest rank, shared with Commander Riker.
Career Path: Science and Medical
As depicted by her blue uniform, Crusher has followed a science/medical career path through Starfleet.
Job Title and Roles During her Tour on Enterprise D
Department: Medical, Role: Head of Department, Primary Job Role
Crusher's Starfleet rank qualifies her to perform all the roles and duties of a Department Head and her professional qualifications and long practicing experience make her ideal for heading the medical department.
Crusher is a fully certified "Bridge Officer" (similar to Officer on Deck, See Richard's and paul's answers) allowing her to act as the ship's commanding officer. While the Bridge Officers do appear to have an informally agreed structure, we can presume that those following the Command, Technical or Tactical career paths are more professionally qualified, but as a Commander she would have the military authority to pull rank on and give orders to almost anyone (save Riker and Picard).
Special Role: Chief Medical Officer
As the highest ranking medical officer onboard, Crusher is also granted the position of Chief Medical Officer (CMO). The CMO, similar to how the position/role of Captain is not the same as the rank of Captain, is a special role mandated by Starfleet for their vessels. The CMO has the authority to remove anyone from active duty if they are deemed, medically, to be unfit.
This is a separate power from her regular powers a Commander, it does not give her the power to promote or demote people above her station, but it does mean if the Captain goes mad there is someone on-board who, technically, can remove him from office and allow the next ranked officer to fill the role until starfleet assigns a replacement.
This role is as accountable to Starfleet as the Captain and is a bit of a double edged sword. Remove the Captain when it's unnecessary, or worse, fail to remove him when necessary and you could find yourself at a court martial. Also as this is technically a power outside of the normal rank structure the CMO would need support from key members of the crew (e. g. The Executive Officer (another special role), who is Commander Riker on the Enterprise).
The CMO position is not granted by rank nor by Primary Job Role. for example despite the fact that Voyagers EMH lacks a formal military rank, he is technically CMO as he is the only member of the crew left with high levels of medical training and, as a computer program, could be given formal officer training quickly.
To correlate Navy terms, you can have a ship with an officer of the Medical Corp or the Supply Corp, etc. and have that officer even outrank the Commanding Officer by merit of rank, but the CO is a Line Officer and holds the title of CO so even if he is of a lower rank he would still hold lawful authority. Crusher is not a 'line officer' like Riker and Picard, she is a Medical officer so while her rank probably merits a higher pay and the privileges of being higher ranked (like saluting and boarding formalities), her title limits her scope of authority.
Example, An Admiral of the Dental Corp could not just walk on to a warship (starship) and take command just because the Commanding Officer is just a rank of Captain or Commander. However, that Admiral will get "ringed on" to announce his presence and he likely makes way more money than the CO.