Nightmare Moon

Nightmare Moon is one of the villains in My Little Pony: Friendship is Magic, and the main antagonist of the two-part pilot episode "Friendship is Magic" also known as "Mare in the Moon". Before that, she was known as Princess Luna, and is the younger sister of Princess Celestia.

History
Long ago, Luna and Celestia were both the rulers of Equestria, a land inhabited by ponies. Every morning, Celestia would raise the sun, and every evening, Luna would raise the moon. Together, they kept the balance perfectly. One day, however, Luna grew jealous: the ponies would be awake to enjoy the day and love, admire, and appreciate Celestia's work, but would always sleep through her far more beautiful night. One day, Luna turned on Celestia, saying she should be the only princess in Equestria, and raised the moon before the bitterness in her heart manifested and transformed her into Nightmare Moon. Celestia did not want to fight Nightmare Moon and told her to lower the moon, but Nightmare Moon said her only royal duty was to destroy Celestia, and shot at her with magic beams, eventually knocking her down. Seeing no other way, Celestia reluctantly used the Elements of Harmony to overpower Nightmare Moon and banish her to the moon, creating the phenomenon known as "the Mare in the Moon" (a unicorn's face imprinted upon the surface of the moon). It is said that in a thousand years, on the longest day of that year, Nightmare Moon will make her return.

Nightmare Moon's return and redemption
Upon reading an ancient legend, Twilight Sparkle realized Nightmare Moon is about to return, however her mentor, Princess Celestia, encouraged her to make friends instead of worrying about the supposed "old pony's tale". Unfortunately, Twilight turned out to be right and Nightmare Moon was released during the Summer Sun Celebration, replacing Celestia who was supposed to attend it. Twilight and her soon-to-be friends chased Nightmare Moon into the forbidden Everfree Forest.

After conquering many obstacles left by Nightmare Moon in their path, they finally found the twisted mare herself along with the ancient Elements of Harmony, now reduced to inert stones. As Nightmare Moon whisked the Elements away, Twilight followed her into the portal and confronted her alone. She tried to use the Elements of Harmony like Celestia did, but for that she had to find the "spark" that would make a sixth Element appear. However, no matter how hard she tried, she couldn't make the spark, and Nightmare Moon destroyed the Elements.

Fortunately, when Twilight heard her new pony friends calling for her, she felt a different spark in her heart; making her realize that the real Elements are not just some stones: they exist in the hearts of her friends. That spark triggered the final element: Magic (also known, of course, as Friendship), and reanimated the shattered Elements, converting them into jewels for the six friends to bear. Using the Elements of Harmony, Twilight activated a spell that struck Nightmare Moon, exercising the darkness from her and transforming her back into Princess Luna.

Celestia arrived in the ruins where the battle took place, admitting that she knew of Nightmare Moon's return, but knew the only way to defeat her was for Twilight to understand the importance of friendship. Celestia walked over to a cowering Luna, who apologized in tears for her actions, and was granted forgiveness. Later at the celebration (held by Pinkie Pie), even the other ponies forgave Luna, gifting her with a flower necklace.

Nightmare Night
In the season 2 episode "Luna Eclipsed", Princess Luna, now uncorrupted (And with a new look. Lauren Faust, the show's creator, stated that this was because when Nightmare Moon was defeated, Luna was reborn with very low magic levels, and she transformed after getting her good powers back), returns to Ponyville during the holiday Nightmare Night, a holiday dedicated to Nightmare Moon depicting her as a scary bogeyman. Luna, with Twilight's help, tries to change her image from that of the terrifying Nightmare Moon, to a favorable one as Princess. Unfortunately, the nature of the holiday, combined with Luna's outdated mannerisms and flair for the dramatic, make her subjects more scared of her than ever. Her first attempts end disastrously, prompting her to decree Nightmare Night canceled, but Twilight persuades her to give Ponyville another chance. When Luna discovers that the residents actually enjoy being scared as part of the night's festivities, she uses the Nightmare Moon persona to help them have fun and thus gains their trust and friendship.

Season 4
During the Season 4 premiere two-parter "Princess Twilight Sparkle", Twilight attempts to figure out the source of the chaos seizing Equestria by drinking a potion which gives her flashbacks of the past. One of the flashbacks she experiences is of Luna becoming Nightmare Moon and assaulting Princess Celestia. At first Celestia is reluctant to fight her sister, but Nightmare Moon assaults her with dark beams and eventually wounds her, sending her plummeting to the ground. However, Celestia promptly recovers and arms herself with the Elements of Harmony, the power of which she channels into a powerful beam that overwhelms Nightmare Moon's defenses and seals her inside the moon.

Other appearances
Luna has a few cameos in the season 2 finale A Canterlot Wedding, flying in to guard Canterlot at nighttime, ironically not participating in the fight against the changelings for reasons unknown. After Chrysalis was defeated, Luna appeared next to Celestia and the Mane 6, asking if she missed anything.

She also appears in the season premier The Crystal Empire.

At the beginning of Seasons 2 and 3, it's revealed that Luna, alongside Celestia, turned Discord to stone with the Elements of Harmony and imprisoned King Sombra in ice while turning him to shadow, about a thousand years ago sometime before her transformation into Nightmare Moon.

Nightmare Moon in comics
In Issue 6 (Part 2 of the second arc), the nightmare forces turn Rarity into a new Nightmare Moon called Nightmare Rarity, possessed by their darkness. It is also said that the nightmare forces told Nightmare Moon/Luna what to do to make ponies fear her, that she promised them a kingdom of their own, and Luna thought they would die with the evil of Nightmare Moon gone, but they instead returned to the moon for one more chance in the cycle of the new moon.

In Issue 7, Nightmare Rarity imprisons the ponies in her dungeon once the Nightmare Forces defeat them, and resists Spike's attempts to get through to her. She tries to trick him with an illusion, but he realizes her deception. She has Shadowfright and his sidekick capture him and says the nightmare has only begun.

In Issue 8, the ponies escape the dungeon and decorrupt Shadowfright's sidekick, Jerome, with the power of friendship. Luna does the same to Shadowfright and his army, turning them back into the denizens of the moon. The ponies then use this power of friendship on Nightmare Rarity to weaken her, and with this, them thinking of memories of Rarity, and Spike's love for her along with his restored Fire Ruby, the Nightmare Energy and spirit of Nightmare Moon are destroyed, releasing Rarity from their control.

Nightmare Rarity speaks as if she were the original Nightmare Moon, and speaks of Rarity as a separate character, hinting that Nightmare Moon is the manifestation of Luna's bitterness magnified by or merging with the Nightmare Forces, returned through the cycle of the new moon to possess Rarity.

Luna appears in the comics as well, though not as Nightmare Moon, and with her mane and tail the way they were after she was restored.

Note: It is up for the fans to decide whether the comics are canon.