After Filing the Application
Once an examiner is finished scrutinizing the application, he or she outlines all of its problems and sends out a first Office action (which basically serves to state that the application has been rejected). The examiner explicitly states under what grounds the application is being rejected under in this Office action.
Perhaps the invention isn't a patentable object. Perhaps it's just too obvious or it isn't novel. No matter what the problem, the examiner will be sure to let you know it exists.
Examiners use a key to indicate the exact reasons why an application is rejected. For example, if your application was rejected due to basic patentability issues (you tried to patent a law of physics), the examiner will reject your application under 35 U.S.C. 101. There are many other reasons with corresponding codes.
You, or your patent attorney or agent will need to respond to the Office Action and try to overcome the rejections. You may receive a 2nd Office Action where you will once again attempt to overcome the rejections and get closer to its allowance. If you send in a reply to a 2nd Office Action and the examiner still does not believe your patent application should be granted a patent, then he or she will send out a Final Rejection. In most instances this is the end of the patent application. Note that while it is possible to Appeal, this process is expensive and should be considered carefully, you do not want to drag your application on unless it may really qualify for a patent.