Why do Jews believe they can hasten the coming of the messiah even though they failed to stop the destruction of both the first and second Beis Hamikdash?
All of the other answers are incredibly missing the point. This is an explicit Gemara (Sanhedrin 98a), based on Yeshaya 60:22.
אמר רבי אלכסנדרי רבי יהושע בן לוי רמי כתיב (ישעיהו ס, כב) בעתה וכתיב אחישנה זכו אחישנה לא זכו בעתה
Says R’ Alexandri: R’ Yehoshua Ben Levi posed a contradiction. It is written “[Mashiach will come] in its time,” and it is also written, “I will hasten it.” [He settles the contradiction:] If they merit it, “I will hasten it.” If they don’t merit it, [it will come] “in its time.”
Why do we believe we can hasten Mashiach? Because G-d Himself said we can.
there is a concept that each generation rectifies a bit and this rectification remains intact. i.e. even if they sin, it does not destroy what was rectified. so each generation fixes a bit more until eventually it is enough to bring the third temple.
secondly, God commanded us on this and promised us it's coming through his prophets thus as the Ramchal writes in the Path of the Just (ch.19)
"it is Zion; no one inquires after her" (Jeremiah 30:17), which our sages expounded: "this implies it needs inquiring after" (Sukkah 41a). Thus we learn from here that we are obligated in this matter, and cannot exempt ourselves due to our lack of power. For on all such matters, we learned: "It is not incumbent upon you to complete the task, but neither are you free to abstain from it" (Avot 2:16).
Because we messed up again and again doesn’t mean we should give up. God believes in the potential of future generations to achieve what their anscestors did not.
אוְהָיָה֩ כִֽי־יָבֹ֨אוּ עָלֶ֜יךָ כָּל־הַדְּבָרִ֣ים הָאֵ֗לֶּה הַבְּרָכָה֙ וְהַקְּלָלָ֔ה אֲשֶׁ֥ר נָתַ֖תִּי לְפָנֶ֑יךָ וַֽהֲשֵֽׁבֹתָ֙ אֶל־לְבָבֶ֔ךָ בְּכָ֨ל־הַגּוֹיִ֔ם אֲשֶׁ֧ר הִדִּיחֲךָ֛ יְהֹוָ֥ה אֱלֹהֶ֖יךָ שָֽׁמָּה: 2and you will return to the Lord, your God, with all your heart and with all your soul, and you will listen to His voice according to all that I am commanding you this day you and your children, בוְשַׁבְתָּ֞ עַד־יְהֹוָ֤ה אֱלֹהֶ֨יךָ֙ וְשָֽׁמַעְתָּ֣ בְקֹל֔וֹ כְּכֹ֛ל אֲשֶׁר־אָֽנֹכִ֥י מְצַוְּךָ֖ הַיּ֑וֹם אַתָּ֣ה וּבָנֶ֔יךָ בְּכָל־לְבָבְךָ֖ וּבְכָל־נַפְשֶֽׁךָ: 3then, the Lord, your God, will bring back your exiles, and He will have mercy upon you. He will once again gather you from all the nations, where the Lord, your God, had dispersed you.
Or as it says in Tehillim 68:
לְמַ֚עַן יֵֽדְע֨וּ | דּ֣וֹר אַֽ֖חֲרוֹן בָּנִ֣ים יִוָּלֵ֑דוּ יָ֜קֻ֗מוּ וִֽיסַפְּר֥וּ לִבְנֵיהֶֽם:In order that the last generation might know, sons who will be born should tell their sons. 7And they should put their hope in God, and not forget the deeds of God, and keep His commandments. זוְיָשִׂ֥ימוּ בֵֽאלֹהִ֗ים כִּ֫סְלָ֥ם וְלֹ֣א יִ֖שְׁכְּחוּ מַֽעַלְלֵי־אֵ֑ל וּמִצְוֹתָ֥יו יִנְצֹֽרוּ: 8And they should not be as their forefathers, a stubborn and rebellious generation, who did not prepare its heart and whose spirit was not faithful to God. חוְלֹ֚א יִֽהְי֨וּ | כַּֽאֲבוֹתָ֗ם דּוֹר֘ סוֹרֵ֪ר וּמֹ֫רֶ֥ה דּ֖וֹר לֹֽא־הֵכִ֣ין לִבּ֑וֹ וְלֹֽא־נֶֽאֶמְנָ֖ה אֶת־אֵ֣ל רוּחֽוֹ:
We believe we can hasten the coming of Moshiach, meaning the final redemption because the entire Jewish people were present at Sinai and accepted the whole Torah, written and oral. And one of the teachings of the oral Torah is that fulfilling the mitzvot, in particular the mitzvah of Tzedakah, which is called the mitzvah in Yerushalmi Peah 1:1 because it is equated with all the mitzvot in the Torah
וצדקה תציל ממות ולא מית אלא שלא ימות לעולם הבא צדקה וגמילת חסדים שקולות כנגד כל מצותיה של תורה
, hastens the redemption, like is found on Bava Bathra 10a.
It is taught in a baraita that Rabbi Yehuda says: Great is charity in that it advances the redemption, as it is stated: “So said the Lord, uphold justice and do charity, for My salvation is near to come, and My righteousness to be revealed” (Isaiah 56:1).
The destruction of the first two Temples, or sinning in general, is not a factor in that relationship. And in fact, that doubt arises from the presumption that G-d did not intend for sin. But the Torah teaches that this type of belief is incorrect.
The Torah preceded the creation of the universe by 2000 years like is found in Midrash. And it is written in the Torah that Adam sinned. That means all these consequences of sin, including the destruction of the two Temples, are intended too from before the creation of the universe. And all of it is for the good like is said in the Amidah prayer three times each day כי אל טוב ומטיב אתה.
I think you might attain a significant perspective on this issue by watching this interview segment with the late Prof. Yesha'yahu Leibowicz:
The subtitles do not quite express what he's saying though, so let me restate it:
Q: And you, do you believe in the coming of the Messiah?
A: I am one of those people who believe the Messiah will come [Leibowicz puts extra stress on the prefix "YA", which indicates the future tense].
A: [shouting] will [again, stress on the "YA" prefix] come. For ever. A messiah which has come is a false messiah. Any Messiah who comes is a false messiah, since the essence of the Messiah is in that he will come.
So (my non-believer interpretation now) the coming of the Messiah is something like the horizon - it never actually materializes, but a feature of your existence is that you sense it in the distance. And thus, expectation of the Messiah is an existential state which does not go away.
Thus, hastening the coming of the Messiah does not mean that you expect him to arrive in 100 years and with your Tzdaka or with your devoutness you would shave off 5 years or 20 years (or just a day), so it will now be 95 or 80 years or whatever. It's that the existential state of expectation is improved, i.e. your life or the world is in the state of the Messiah more hastily approaching than less hastily approaching. It is certainly a great thing to live during times at which the Messiah approaches with haste rather than very slowly - wouldn't you say?
PS 1: So whatever happened or didn't happen with the temples doesn't have any bearing on it.
PS 2: Leibowitz was a philosopher, not an accepted authority on Halacha nor any other position in established Judaism.