How it basicall works is that you take all your classes, do your major rotations and take Step 1 of the board, and based on your class and rotation grades and your USMLE score you have a good feel for how competitive you are.
You decide what specialty you would like to pursue and apply to some residencies in those programs, keeping in mind that if you have a low board score, you won't likely fare well in the more competitive subspecialties.
Your advisor will help you decide what residency programs to apply to and how many. The application process is very similar to med school, with lots of fees, forms, and interviews.
You then go to about 10 interviews (on average), and see each of the residency programs. After you're finished, you get on a computer and rank each of the schools you interviewed at and would go to from the one you want the most to the least. Each residency program will do the same with their applicants.
At that point, all the information gets put into a computer, and the computer spits out a match for you. Most schools have a "match day" in March where each student gets an envelope with thir residency match and you open them together. You then go to your residency wherever your match was.
If by some chance you did not match at any of your schools you ranked, a couple of days before match day the people will call you and tell you so. You will then have an opportunity to enter a scramble match, which is where they quickly place you in a residency program that did not fill its slots.
You may end up in a location or even specialty that is not ideal if that happens. Many people who do not match choose to sit out a year and try the match the following year.
In most cases, as long as you apply reasonably for your situation, you should match. However, if you had bad LORs, a questionable grade, or particularly a low board score, and you applied for a competitve location (ie MassGen residency), or a super-competitive residency (dermatology, radiology, neurosurgery, ENT, etc), you run the risk of not matching.
That's where great advisors come into play, as they will let you know where you should realistically be applying.