I can answer how it works. Unless it's changed recently, your class rank is 60% of your academic performance, 25% military performance, and 15% physical fitness. Your academic score comes from your academic GPA, military performance is determined by your Cadet Evaluation Reports scores (cadets receive a CER each semester and then one in the summer), and your physical fitness is determined by your Physical Fitness Exam scores (taken twice a year, August and January). Overall, where your assigned is based on 1) Your Class Rank and 2) Location/Unit Popularity. For example, for my class (2014), some of the lowest ranking people were assigned to Hawaii and the number two cadet and anchor (last in the class) cadet were assigned to Alaska. Other years, the reverse is the same. Sometimes, Virginia is popular because there are a lot of vessels concentrated there and its easier to get assigned with your future spouse (co-locate) or friends (unofficial term "bro-locate").
Most people, approximately 85%, will go to a cutter. You will not get an ashore billet unless you're in the top 30 - more than likely the top 15 - of the class. Ashore billets make up about 5% of a class leaving 10% to aviation. For aviation, class rank matters but not as much as for ashore or a cutter. That's because flight school requires you to submit your class rank, take a written exam, interview with a flight board, and pass a flight physical. There have been applicants who graduated at the top of their class that did not receive orders to flight school as well as applicants towards the bottom of the class who did. Your major does not matter other than Engineers are encouraged to go afloat as Engineering Officers in Training (EOITs). However, engineers can also choose to be Deck Watch Officers (navigation/ship driving) and non-engineering majors can be EOITs.