Simplest answers, without going into a lot of detail:
Active duty military are paid twice a month.
Officers resign their commission by submitting a formatted request asking for separation (if not retiring after the requisite number of years) or retirement. This has to be submitted a specified amount of time in advance to allow for processing and to order in your replacement. It's not two weeks.
It's usually a certain number of months.
The release date may not be exactly on your obligation year anniversary; it may fall a bit later for a number of operational reasons. Depending on your warfare specialty, the payback clock may start after requisite training.
Generally, the Service lets you go as requested if service obligation has been met. If, however, certain exigent circumstances exist, you can be involuntarily retained. Much depends on whether the type of commission you obtain is, say, USN or USNR. This is a very simplistic explanation, but someone with a Regular commission may continue to serve after their obligated service period ends and if their performance is satisfactory, unless the Service forces them out for manpower numbers management reasons. Those with Regular commissions may not be released when requested if the Service requires then. Someone with a Reserve commission might like to stay past their obligated service date, but the Service may choose to release them and not issue additional orders. These are types of commissions, both held by people on active duty, not to be confused with serving in the Reserves. An officer serving on active duty may submit, after a specified period of time, a request to be "augmented" into the Regular Navy, and their commission type will change from USNR to USN, or it may be offered to them at some point. I received a letter at the two year point, saying I was being augmented due to superior performance, unless I wished to decline in writing.
SA grads receive Regular commissions, though there was a period they did not. Not sure of ROTC or OCS/OTC - the Service changes things up occasionally. What this means is, down the road, a top-performing junior officer with a USNR commission may be released before a mediocre officer with a USN commission, if the Service is cutting end strength. That's getting into advanced "CO's nightmare" territory.
If you want to be separated before your obligated commitment (two words that give a powerful clue) is up, it can be requested, perhaps on humanitarian discharge grounds, but with little likelihood of being approved, unless it suits your Service's needs. The deal you sign up for is X years of your life in 24/7 service for Y years of education. Occasionally a Service needs to cut some numbers and will allow a certain number to separate before their obligated service is complete; that is always announced very publicly and specifically.
You will get plenty of briefs on this before officially committing.