This was not a Star Trek episode. It was an episode of Stargate SG1 – Unending – the last episode of the series. From the Wikipedia entry:
The members of SG-1 and General Hank Landry (Beau Bridges) are travelling on the Earth ship Odyssey to the Asgard home world, Orilla, when the Asgard Thor beams aboard. He reveals that after millennia of genetic manipulation, a disease has brought his race very close to extinction, and SG-1 accepts his offer to upload the sum of all Asgard knowledge into Odyssey as a way to preserve the Asgard legacy after their mass suicide. However, Orilla and Odyssey are soon attacked by Ori warships. Orilla erupts with huge explosions, and after Odyssey escapes to the next planet with a Stargate and beams the bulk of its crew down, the Ori fire a final energy beam upon the ship. Lieutenant-Colonel Samantha Carter (Amanda Tapping) activates a localized time dilation field that makes time look frozen outside the field to give SG-1 and Landry time to find a defense.
During the initial months on board, Vala Mal Doran (Claudia Black) repeatedly tries to seduce Daniel Jackson (Michael Shanks). Daniel finally confronts Vala for what he believes to be her insincerity, teasing, and mocking of him. Vala's upset reaction makes Daniel realize the honesty of her feelings, and they share a passionate embrace. As the years pass, each team member attempts to deal with the isolation. General Landry develops a gardening hobby; Carter learns how to play the cello; Daniel continues the translation of Asgard information in the database and develops his relationship with Vala; and Cameron Mitchell (Ben Browder) and Teal'c (Christopher Judge) exercise and train while Mitchell is growing more frustrated and angry. After many years, General Landry succumbs to old age and dies.
After fifty years Carter has devised a way to reverse time within a localized field, however the Odyssey's power source (ZPM) is almost completely depleted from maintaining the time dilation field for fifty years. Mitchell however hypothesizes that the power of the Ori energy beam could provide the required energy to reverse the time. This, however, will mean the loss of any memories of the last fifty years; additionally, whoever volunteers will remain at their advanced age. Teal'c, who as a Jaffa has a much longer lifespan than the other team members, volunteers to remain behind and perform the rescue, protected within a separate field. As the group prepares their plan, Vala and Daniel exchange a last embrace, assuring each other of their love. When everything is in place, they deactivate the time dilation field and, after the normal timeline is restored, Teal'c prevents Colonel Carter from activating the time dilation field. Odyssey leaves before it is destroyed, saving SG-1 and the Asgard's legacy.
Back at the SGC (Stargate Command), Teal'c refuses to reveal any of the events on the ship, much to Vala's disappointment. As the episode, the season and the series come to a close, SG-1 contemplates Teal'c's joking words of wisdom in the gateroom and says "indeed" all at once. General Landry wishes the team God-speed, and SG-1 steps through the gate on their next mission."