What did Er do that was evil in the Eyes of Hashem?
Ramban: He died on account of his unspecified sin or sins.
Rashi: Which he did in order not to spoil Tamar's beauty, should she fall pregnant.
Riva, Moshav Zekenim (9), Hadar Zekenim (9): Er and Onan had Bi'ah Lo k'Darkah (Yevamos 34a). Tamar removed her Besulim by hand, therefore she was able to become pregnant through her first normal Bi'ah [with Yehudah]. Alternatively, they had Bi'ah in the normal place, but without breaking the Besulim (Riva, Hadar Zekenim). R. Menachem asked why Er did not have normal Bi'ah the first time. It cannot impregnate! (Perhaps he was concerned lest she was not a virgin, or she removed her Besulim by hand! - PF)
Why were Er and Onen punished? They were not old enough to be liable [bi'Yedei Adam], and Shamayim does not punish below the age of 20 (Shabbos 89b)!
Moshav Zekenim: Shamayim judges a person according to his Chochmah. If a minor has Chochmah like a 20 year old 1 , he is punished. 2 We find that Eli wanted to punish (kill) Shmuel for ruling in front of him, even though Shmuel was only two years old. Also here, even though Er and Onan were only eight or nine, since they were able to father children, they are considered adults. 3
Chacham Tzvi (49) and many others challenge this (that Shamayim does not punish below 20). Teshuvas Chasam Sofer (YD 155) limits it to special cases, e.g. Onesh Dor ha'Midbar. Tzitz Eliezer (14:20:2) brings many Poskim, and proves that the Halachah does not follow this Agadah.
Hadar Zekenim (10): The nation is punished bi'Ydei Shamayim based on its majority only from the age of 20 (e.g. for heeding the Meraglim), but individuals, even below 13, and similarly for good. (For good, minors are rewarded even as a nation. The world stands on the Torah of children (Shabbos 119b). - PF)
Riva (10) and Hadar Zekenim (10) support this from Shlomo. He was at most 12 when David told him "if you abandon Hashem, He will abandon you forever"!
The Rosh asks so also about Tamar (even though she was a minor, Yehudah planned to execute her) and answers for all three, since they could have children, they are like adults. (How could Yehudah know that she was old enough to bear children? Perhaps she was pregnant with a Nefel! - PF. The Rosh's text in Sotah (10a) must say "I am a minor", like Hadar Zekenim (14) says, unlike our text. According to Rashi (24, she was Shem's daughter), she was an adult, since Yakov returned to Eretz Yisrael in 2207, 49 years after Shem died.)
See Oznayim la'Torah DH 'Bechor Yehudah' in the name of the Maharsha.
Why does it say that Er was Yehudah's Bechor?
Malbim: Malchus Beis David was proper to come from him. If not [for his great potential], he would not have been bad in Hashem's eyes.
Ha'Emek Davar: Malchus Beis David should have come from Yehudah's Bechor. Er was not worthy of this, so Hashem killed him.