Trains from the city run as one set until Ohlsdorf, where they are split: the front three carriages go to the airport and the rear three carry on to Poppenbüttel, a completely different part of town. The on-board displays and the platform announcements say which half is which, but it is easy to board the wrong end at the Hauptbahnhof and end up in the suburbs. Coming in from the airport you need to do nothing — the two halves re-couple at Ohlsdorf and continue to the city.
Hamburg's short-trip fare is €2.10 and only covers up to three stops on bus and U-Bahn or two stops on the faster S-Bahn and regional lines, with no changes. The airport is ten stops from the Hauptbahnhof. The correct ticket is the Hamburg AB single at €4.10 (€3.81 in the app), which is valid for one journey in one direction with transfers. There are no ticket gates, but inspectors work the S1 and the penalty fare is charged per person.
The last S1 leaves the airport at about 00:13 and the first arrives from the Hauptbahnhof at about 04:32. In between there is no train to the airport at all. On weeknights the 606 night bus connects the airport with the city centre; at weekends the 274 runs the airport to Ohlsdorf leg, where the S1 and U1 continue all night. The airport itself also has a night flight ban — scheduled operations run 06:00 to 23:00 — so a 02:00 arrival is rarer than at most European airports, and the bus is the only way out.
Hamburg publishes flat rates that a driver may apply on request: €38 up to 12 km, €52 up to 20 km, €82 up to 35 km. The airport is about 10 km from the Hauptbahnhof, where the meter normally lands near €35, so asking for the €38 flat rate costs you money. Separately there is a fixed €8.00 surcharge for a large taxi carrying more than four passengers or a bike carrier — worth knowing if you are a group of five.