Usually I can add the Advanced Passenger Information along with my passport# after booking my reservation and I do this via the airline's website when I go to select the seats and request special meals etc.
The machine readable passports store all the information and the Ticket Counter Agent swipes the passport which will populate the information on the screen confirming if its all correct. Then the Agent presses a key to spit out the boarding passes with an "API or DOCS OK" message so you will not need to present yourself at the gate.
The API information needs to be entered in advance of boarding the flight whether you do it yourself online after purchase, during online checkin or at the airport it just needs to be done before you board as the manifest is then sent to the proper authorities at US Customs, Department of Homeland Security, European authorities.
If you are on a no fly list the system will show an error message like"API Error" or Passenger Error" cannot proceed with cehckin or Enter Redress# with a supervisor needing to be called who will then in turn call the authorities to validate or deny your travel.
As for ESTA when you go online to get your ESTA its linked to your passport. This must be done at least 72 hours prior to travel. While you may be able to get it later than that I would not risk it. The agent will see that your ESTA has been approved. If you do not have an ESTA or if its not approved you will then be denied boarding and you may be switched to a later flight while you work out the issue.
I hope this helps.