Speaking strictly movie canon...
Regarding The Primes (per TF wiki):
Eons ago, the AllSpark created thirteen beings, the first Cybertronians, who had the ability to move between dimensions.
According to Jetfire, the Dynasty of Primes was formed by the first Thirteen Cybertronians. These beings could not transform, but they and their transforming descendants possessed powers beyond that of the average Cybertronian.
But not every Transformer is a "genetic" descendent of the Primes. The Primes were just the thirteen originals created.
They soon realized that the AllSpark's power, though vast, was not infinite, and required the energy of stars to be replenished. Once a nearby star went nova, the AllSpark created a worker race, the Transformers, who would assist the Dynasty in replenishing the AllSpark.
Sentinel Prime was not one of the original thirteen. He was a direct descendant.
Sentinel Prime was a direct descendant of the first Cybertronian, Primus, and was believed to be the last of the Primes.
In the movie continuity, Optimus Prime was originally named "Optimus". He and Megatron worked with Sentinel Prime.
[Optimus] was the humble leader of Cybertron's science division, which studied the AllSpark, and were protected by Lord High Protector Megatron's military.
[Megatron] was once brothers-in-arms with Optimus and a student of Sentinel Prime, appointed to be Cybertron's Protector and commander of its Defense Force.
But there was more to Optimus than meets the eye. Optimus is actually a direct descendant of one of the Primes (Prima, to be specific).
It was Optimus Prime's ancestor who finally vanquished the Fallen to another dimension in the battle for the Matrix of Leadership, but his orphaned descendant who was hidden away from the Fallen grew up with no knowledge of what he was.
Sentinel Prime told Optimus of his theories about Prime's lineage
Though Optimus was too humble to accept, not believing Sentinel's claims about his heritage, Megatron overheard their conversation and became jealous of his "brother." Sentinel formulated another plan for Cybertron's future: it would be governed by Optimus's Science Division and Megatron's Defense Force. By having Optimus investigate the remains of the Primes' civilization, it would encourage him to believe in his lineage.
Eventually, Optimus finds proof that he is a descendant of a Prime. It is then that the other Autobots begin addressing him as such, and he becomes known as "Optimus Prime".
Back at Burthov, he came upon Ratchet, Arcee, and Elita-One scanning the pieces of the other artifacts. Arcee then discovered that the pieces all contained the same symbol on them, and that the same symbol was on Optimus' head. Ratchet declared that the symbol was proof of the Dynasty of Primes, and it indicated Optimus himself was a Prime. Ratchet and the other Autobots began to dub him "Optimus Prime" [snip] Optimus also informed Sentinel of the marking on his head that proved he was right all along: he was a Prime too.
Megatron was not a direct descendant of a Prime. He was jealous of Optimus.
But Megatron resented his comrade, knowing he was a Prime and therefore Sentinel's favored son.
Even still, Optimus and Megatron led Cybertron for a time.
For two generations, Optimus and Megatron ruled Cybertron together. The two personalities created a balance; Prime was fair while Megatron was firm.
In one of the comics, Barricade breaks the fourth wall and explains that the two are not actual "brothers" despite each one regarding the other as one.
However, in the twelfth issue of Titan's Revenge of the Fallen comic, "Barricade" informed readers that Megatron and Optimus weren't related, the term "brother" was just because they'd known each other for so long.
Putting this all together, we've painted the picture that Optimus and Megatron were once close allies. They worked together under Sentinel to bring peace to Cybertron. While Optimus was a descendant of a Prime, Megatron was not. Still, from years of friendship and being allies, they considered each other as "brothers" despite not being "genetically" related.