Last time I went to Germany and researched this in detail (about a month ago) the answer was "Sample must have been taken no more than 48 hours before the scheduled arrival in Germany"
The negative test result was checked 4 times! During initial check in for the entire trip, before boarding the flight to Germany, when exiting the plane in Germany right at the gate, and at German immigration.
The time window: Earliest you can take the sample is 48 hours before arrival in Germany AND you need the test result at time of check in for the first leg of your itinerary.
For a long trip with layovers this makes the timing very difficult especially if time zone difference takes a sizable chunk of additional time out of the testing window. In my case 48 hours before arrival was midnight my local time, which isn't particularly helpful.
They prefer PCR tests but an Antigen test (which is much faster) can also acceptable if it meets certain criteria. However, what specific test equipment and labs meet them, and how this needs to be documented is not well defined.
I had an Antigen test result be rejected by one Gesundheitsamt (local health office) and the exact same test result accepted by another one without problems. The two offices are in neighboring cities in the same state, so it's not very predictable. I was lucky enough that one of my PCR tests came in on time to mitigate.
Good source: https://www.auswaertiges-amt.de/en/einreiseundaufenthalt/coronavirus#content_0 and https://www.bundesgesundheitsministerium.de/en/coronavirus-infos-reisende/faq-testpflicht-einreisevo/testing-obligation.html
The second one is specific about the Antigen test requirements and states that
For the competent public health offices to quickly ascertain whether the minimum criteria have been met, the (rapid) antigen test’s manufacturer details must be provided.
The lab that I used had no idea what that means or how to do this. You may have more luck with a testing site at an airport.