The Gemara in Berakhot 3a is the foundational source: Rabbi Eliezer teaches that the night is divided into three watches, and at each watch the Holy One, Blessed be He, sits and roars like a lion over the destruction of the Temple, crying "Woe that I destroyed My house" — and it is at precisely those transitional moments that prayer over the churban and the exile is most fitting.
Shulchan Arukh, Orach Chayim 1:2 codifies this directly: one who rises early to beseech his Creator should time his prayer to the shifts of the watches — at one third of the night, at two thirds, and at the night's end — and the prayer recited at those hours should concern the destruction of the Beit Hamikdash and the exile; it then adds that it is proper for every G-d fearing person to be aggrieved and worried over the churban.
The Beit Yosef traces this ruling explicitly to the Gemara in Berakhot, noting that the Rosh derived from that passage that it is fitting for every G-d fearing person to pour out supplications over the destruction at those hours, citing the verse 'Arise, cry out in the night, at the beginning of the watches' — and Beit Yosef, Orach Chayim 1:13 records this as the direct source chain behind the Shulchan Arukh's ruling.
Shulchan Arukh HaRav, Orach Chayim 1:8 expands on the reasoning: at the watch-transition times the Holy One remembers the destruction of the Beit Hamikdash, and therefore prayer at those hours over the churban and exile is desirable and close to being accepted — making a G-d fearing person's grief and supplication at that hour especially apt.
Mishnah Berurah 1:9 specifies the kabbalistic refinement: the ideal moment for lamenting the churban is just before chatzos (midnight), after which one should study Torah, and the Kabbalists extolled at length the great virtue of rising at chatzos — a practice already printed in siddurim according to the Arizal.
The Zohar's reading of the midnight moment, as explained by Sulam on Zohar, Lech Lecha 363, depicts chatzos as the point when the north wind stirs, the right and left columns unite, and the Holy One awakens to delight with the righteous — the cosmic backdrop against which the human act of rising to study and mourn carries singular weight.
Recanati on the Torah, Bereshit:180 anchors the practice in David's harp sounding at midnight from the north wind, citing the verse 'At midnight I rise to give thanks to You,' and concludes that chatzos is the fitting time to join with the Congregation of Israel and pray for mercy on her behalf — a reading that ties the personal devotion of chatzos directly to solidarity with Israel's exile.
The broader imperative to mourn the churban — of which the nightly rising is one expression — is grounded in the Gemara's statement in Ta'anit 30b that whoever mourns for Jerusalem will merit to see her future joy, and in Berakhot 31a, where Rabbi Yochanan in the name of Rabbi Shimon ben Yochai rules that one may not fill one's mouth with laughter in exile, since full joy belongs only to the time of redemption.
The verse in Tehillim 137:6 — 'if I do not raise Jerusalem above my highest joy' — supplies the biblical voice of this obligation, while Maamar Mordechai Hilchot Taaniyot U'Vein HaMetzarim 18:27 cites the Shaar HaKavanot to the effect that during the Three Weeks it is a very good and proper practice for every sensitive person to sit in mourning after midday and weep genuinely over the destruction, the afternoon timing linked to the mystical stirring of judgment as the shadows lengthen — the very hour the Temple was set ablaze.