All perfect praise be to Allaah, the Lord of the worlds. I testify that there is none worthy of worship except Allaah and that Muhammad, sallallaahu ‘alayhi wa sallam, is His slave and Messenger.
If a worshipper performed the Taraweeh prayers (night prayers in Ramadan) with the intention of performing Tahajjud (late night prayer performed after a short sleep) afterwards, it is better for him to follow the imam (one who is leading the prayer) in the Taraweeh prayers and, when he performs the Witr prayer, the worshipper should intend to perform the shaf‘ prayer. Then, after the Imaam finishes the Witr prayer, the worshipper should perform another Rak‘ah (unit of prayer). After performing the Tahajjud prayer, he can perform the Witr. Thus, the worshipper would have observed the recommendation of following the Imaam until he finishes the prayer and leaves and the recommendation of delaying the performance of the Witr prayer. The Hanbali scholar, Al-Buhooti said, "If a worshipper intends to perform Tahajjud afterwards, it is recommended for him to perform the Witr prayer after the Tahajjud. The Prophet, sallallaahu ‘alayhi wa sallam, said: "Let the Witr prayer be the last prayer you offer (at night)." [Al-Bukhari and Muslim] However, if he does not intend to perform Tahajjud after the Taraweeh prayers, he should follow the Imaam and perform the Witr prayer with him so as to avail himself of the reward of praying in congregation (with the imam until he finishes the prayer). When a worshipper wants to perform Tahajjud after Taraweeh, he may join the Witr prayer with the Imaam and, after the Imaam finishes, he can perform another Rak‘ah and then perform the Witr prayer after Tahajjud. Thus, he can earn the reward for following the Imaam (until he finishes the prayer) and rendering the Witr prayer one's last prayer of the night as well.
If a worshipper performs Taraweeh individually and is certain that he will wake up for the Tahajjud prayer, it is recommended for him to delay performing the Witr to be performed after Tahajjud. Note that the Ash-shaf‘ prayer refers to the even-numbered night prayers that precede the odd-numbered Witr prayer.
Allaah Knows best.