Praise be to Allah.
It is permissible to offer an obligatory prayer behind an imam who is offering a naafil prayer, and vice versa, according to the more correct scholarly view. This has been explained previously in the answer to question no. 153386.
If the worshipper is praying ‘Isha’ behind an imam who is praying Taraweeh, then he should stand up after his imam says the tasleem and complete what remains of his prayer.
An-Nawawi (may Allah have mercy on him) said:
If a person prays ‘Isha’ behind (an imam who is praying) Taraweeh, that is permissible, then when the imam says the tasleem, he should stand up and complete the remaining two rak‘ahs. End quote.
What is prescribed in qiyaam al-layl (voluntary prayers at night) is to offer the prayers two [rak‘ahs] by two, because the Prophet (blessings and prayers of Allah be upon him) said: “The night prayer is two [rak‘ahs] by two.” Narrated by al-Bukhaari (993) and Muslim (749).
Imam Ahmad interpreted this hadith as meaning that that is obligatory. Hence he said that the prayer is invalid if the worshipper deliberately gets up for a third rak‘ah when praying qiyaam al-layl. Imam Ahmad said: Whoever gets up for a third [rak‘ah] in qiyaam al-layl, it is like his getting up for a third [rak‘ah] in Fajr.
Kashshaaf al-Qinaa‘ (1/480).
The majority of scholars are of the view that it is permissible to pray qiyaam al-layl four [rak‘ahs] by four, and they interpreted this hadith as either referring to what is recommended (mustahabb) and stating that that is preferable, or as meaning that the Prophet (blessings and peace of Allah be upon him) said that because it is easier and lighter for the worshipper, as al-Haafiz Ibn Hajar stated in Fath al-Baari.
Shaykh Ibn ‘Uthaymeen (may Allah have mercy on him) was asked:
If the imam prays three [rak‘ahs] in Taraweeh, what should he do?
If he gets up for a third [rak‘ah] in Taraweeh by mistake, then he should sit down again; even if he has recited al-Faatihah, he should sit back down, and recite the tashahhud and the tasleem, then prostrate twice. Imam Ahmad (may Allah have mercy on him) stated that if a man gets up for a third [rak‘ah] in the night prayer [qiyaam al-layl], it is as if he got up for a third rak‘ah in Fajr prayer, and it is known that if a man gets up for a third rak‘ah in Fajr prayer, he must sit back down, because Fajr cannot be offered with three [rak‘ahs], and similarly the night prayer cannot be more than two [rak‘ahs], because the Prophet (blessings and peace of Allah be upon him) said: “The night prayers are two by two.” I have heard that some imams, if they get up to pray a third [rak‘ah] by mistake and are reminded of it, they carry on and pray four [rak‘ahs]. This in fact is ignorance on their part, and is contrary to the words of the Prophet (blessings and peace of Allah be upon him): “The night prayers are two by two.” What should be done is, if a person is reminded in the night prayer or in Taraweeh, even if that is after he has started to recite [Qur’an, in the third rak‘ah], he must sit back down and recite at-tahhiyyaat, and say the tasleem, then do the two prostrations of forgetfulness, and say the tasleem again.
Based on that, this imam – when he remembered that this was the third [rak‘ah] – should have sat down, then done the prostration of forgetfulness at the end of his prayer.
But what this imam did of making the prayer four [rak‘ahs] after getting up for a third [rak‘ah] by mistake is permissible according to some of the scholars.
The Shaafa‘i scholars stated that one who gets up for a third [rak‘ah] in a naafil prayer by mistake should sit down, then do the prostration of forgetfulness.
So if he decides that he wants to add something to his prayer after getting up, then the correct view in their opinion is that he should sit down, then stand up for the third [rak‘ah], so that he will have intended to do more before starting to do it.
Some of the scholars regarded it as permissible to form the intention of adding something to the prayer after getting up for it, and said that the worshipper does not have to sit down in that case.
See: Tuhfat al-Muhtaaj (1/271).
Based on that, what the imam did is permissible according to some of the scholars, and if the imam does something that is permissible according to some of the scholars, based on his own ijtihad or following a scholar or thinking that it is valid, then his prayer is valid, and the one who is praying behind him must follow him.
Shaykh al-Islam Ibn Taymiyah (may Allah have mercy on him) said:
If the imam does something that in the view of the one who is praying behind him is forbidden in prayer, and it is a matter concerning which there are different views, the prayer offered behind him is valid. This is the well-known view narrated from Ahmad. He said: The reports narrated from Ahmad indicate the same thing; however, they appear to suggest that in every situation where it is certain that the imam made a mistake, then the prayer must be repeated, and when that is not certain, it does not have to be repeated. This is what is indicated by the reports and by usool al-fiqh. However, there are other scholarly views concerning this issue.
Al-Ikhtiyaaraat (p. 70).
Based on that, the imam’s prayer was valid, and your praying ‘Isha’ behind him was also valid.
According to the view that adding to the prayer is not right in this situation, if the person praying behind him thought that it was permissible for the imam to do that, or he was unaware of the prohibition on following the imam in the additional rak‘ah, or he did that by mistake, his prayer is still valid.
And Allah knows best.