For the Marine Corps:
For ranks E1-E3 it is dependent on Time in Service and everyone gets it unless you screw up.
E-4 and E-5 is dependent on Time in Service, Time in Grade, PFT score, and rifle score. Every three months, the Corps comes out with what the score that will get you promoted, e.g. 1419. If you meet the score and Time in service and time in grade requirements, you will get promoted. Also, you can get meritoriously promoted meaning, you will get promoted automatically regardless of your TiS or TiG. Usually, battalions will have a meritoriously promotion competition once a year.
E-6 through E-9 is dependent on your Time in Service, Time and Grade, PFT score, rifle score, and a report given by your OiC.(I can't remember the name of it for nothing right now) Once a year, there is a list sent out that has the people that are promoted from each respective rank. The thing is that you will have to wait for your name to be promoted. Meaning, in each MOS, there is only a certain number that get promoted each month. So if your name is 120th on the list and your MOS promote 40 per month, you will have to wait 3 months to actually get the promotion. Some billets, such as Company Gunnery Sergeant or Company First Sergeant, can get frocked.....Meaning, you can wear the rank but you won't get paid for it until you actually are promoted. Marine Corps peeps know what I mean.
Officers are selected similarly to how E-6 through E-9 are selected.