When is the latest time one can daven Maariv/Arvit? Is there a divergence between the letter of the law and an ideal time (לכתחילה/בדיעבד)?
One can pray until dawn (alot hashahar), ideally (lechathila) one should do so before midnight (halachic midnight i.e., hatzot) (Kitzur Shulchan Aruch 70:2).
The time for reciting maariv (the evening prayers) extends throughout the night. Nevertheless, the sages were concerned that people would delay the recitation of the biblically mandated nighttime Shema – which is recited as part of the maariv – until the last moment, and risk forgetting to say it altogether. They therefore instituted that maariv should be prayed before halachic midnight. If this time has passed, one can still pray the complete maariv until alot hashachar (dawn).
If due to unavoidable circumstances alot hashachar has arrived and you have not yet prayed maariv, you may still do so until sunrise. However, omit the blessing beginning with the words "Hashkiveinu avinu" ("Our Father, lie us down..."). In this blessing we ask G‑d to give us a peaceful night, and it would be somewhat silly to request a good night when the day is just beginning!
A practical calendar to see what times this corresponds to in your location can be found here.
The first chapter in Mishnah Brachos discusses the latest time one can daven Maariv. The halacha is that you may daven Maariv from when the stars come out (tzeis hakochavim) until sunrise, and this is brought from a story of Rabban Gamliel's sons returning home late from a feast.