I've heard a lot about Yama who carries dead persons from Earth to Yamaloka in his vehicle, a buffalo. My question is, how was Yama born and how did he become the king of Yamaloka?
Yama is the son of Surya the sun god and his first wife Sanjana. As I discuss in this answer, Vishwakarma the divine architect married his daughter Sanjana to Surya, but she couldn't bear the brilliance of Surya's rays. So once when Sanjana was pregnant with Yama and his twin sister Yamuna, she closed her eyes upon the gaze of her husband, so angered by this Surya cursed her unborn children, as described in chapter 77 of the Markandeya Purana (page 135):
As soon as she was espied by the sun Sajna used to close her eyes and therefore he, in anger, addressed to her cruel words: "Since, O stupid one, you close yours eyes as soon as I cast my looks on you therefore will you give birth to Yama, the destroyer of creatures." Thereupon the goddess, stricken with fear, assumed trembling looks, at which the sun again said to her. "Since beholding me, you have assumed trembling looks you will give birth, as your daughter, to the fickle river." Thus by the [cursed] of her husband she gave birth to Yama and the great river celebrated under the name of Yamuna.
After that Sanjana couldn't stand Surya's brilliance any longer, so she ran away to to earth, but she sent her shadow Chayya back to Surya to pretend to be her. Chayya was really good at acting like Sanjana, and even had several children with Surya, but she treated her children better than Sanjana's children, so Yama once raised his foot to kick her, which led to him incurring another curse (page 136):
The illusory Sajna did not manifest that excessive affection towards the sons and daughter of Sajna as she did towards her own offspring. She daily looked after their own comforts. [Sanjana's first son] Manu forgave her for this but Yama could not do so. Then to strike her he raised up his foot in anger - and then immediately stricken with mercy he did not let it fall on her person. Thereupon, O twice- born one, the illusory Sajna, with her palms trembling and lips expanded in anger, imprecated a curse on Yama: "Since, out of irreverence thou hast raised a foot against me who am thy father's wife thy that very foot shall drop off to-day".
Surya then realized that this woman was not Sanjana, because no mother would issue such a curse against her own son. So Surya found out the truth and he went to the Earth to find Sanjana. Sanjana agreed to come back, after Surya asked Sanjana's father Vishwakarma to his divine tools to reduce Surya's brilliance an eighth. Afterwards Surya lived happily ever after with his two wives Sanjana and Chayya. And he assigned each of his children a job, in particular making Yama the god of death (page 137):
[H]is second son Yama, on account of the [curse] of his mother, became of virtuous looks. His father brought about an end of his [curse] saying, - "The worms, taking the flesh of his feet, shall fall down on the earth." Because he was of virtuous looks and impartial both towards his friends and enemies his father appointed him in the office of the Regent of the Dead.
One more thing: you said that Yama "carries dead persons from earth to yamaloka in his vehicle, a buffalo". But Yama doesn't usually transport souls himself; his attendants the Yamadutas are the ones who usually do that. The Vana Parva of the Mahabharata tells the story of Savitri. When Savitri's husband Satyavan dies, Yama comes to take him, so Savitri asks him, "I had heard that thy emissaries come to take away mortals, O worshipful one! Why then, O lord, hast thou come in person?" Yama says this in response:
This prince is endued with virtues and beauty of person, and is a sea of accomplishments. He deserveth not to be borne away by my emissaries. Therefore is it that I have come personally.
So clearly he's making an exception to the standard procedure.
Matsya Purana Chapter 11 narrates slightly a different story regarding how he became king of Yamaloka.
After getting cursed by Chaya, disguised as Sanjana, Yama develops detachment in his mind and did Tapas for Lord Shiva 20,000 years. Lord Shiva grants him the boon to become Lokapaala and powers to judge Dharma and Adharma.
Chäyä loved her son Manu much more than the other children. This step motherly treatment was tolerated by Manu the son of Sanjfiä, but once Yama was enraged because of this and threatened Chäyä to kick her with his left foot. At this Chäyä pronounced a curse on Yama, "Your foot shall be eaten up by the worms. The blood and puss shall always ooze out of it."
On hearing the curse, the enraged Yama, went to his father and spoke, "O Lord, the enraged mother has pronounced a curse on me for no reason at all. O Lord, because of my childish habit, I had raised my right foot once. At this, she, inspite of dissuading by the elder brother Manu, pronounced a curse on me. Since she has attacked me by means of a curse, therefore she does not appear to be our mother." On hearing this the sun god again said to Yama, "O Intelligent one, what should I do? One has to suffer sometime due to some one's own foolishness. Or otherwise one has to reap the reward of his own karmas. This rule is applicable over Siva himself, then what to speak of others. Therefore, O Son, I am giving you a cock or a peacock, which shall consume all the worms of your foot besides the -blood and the puss emerging out of it."
At these words of the father, the immensely glorious Yama developed detachment in his mind. Then he went to the holy place of Gokarna living on fruits, leaves and the air, engaged himself in severe tapas. In this way, he adored lord Mahädeva for twenty thousand years. In due course of time, the trident bearer Siva, getting pleased with his tapas appeared before him.
Then Yama, in the form of a boon, achieved the position of a Lokapäla, besides the lordship of the manes, and the powers of judgement for the performing of dharma and adharma by the people. Siva, the lord of the universe, bestowed all the boons on him.