There are two types of bekhorim, and both are included in this custom, because both types of bekhorim died in Egypt. The first type is a bekhor to one’s father, the bekhor who inherits a double portion of his father’s estate. Even if his mother bore children from a previous husband, or had an earlier miscarriage (and he is therefore not considered his mother’s firstborn and need not be redeemed by pidyon ha-ben), the custom of fasting applies to him, because he is a bekhor to his father with regard to the law of inheritance.
The second type is a bekhor to one’s mother, the bekhor to whom pidyon ha-ben applies. Customarily, he too observes Ta’anit Bekhorot, even if his father already had children with another woman and he will not inherit the double portion. He has the status of “petter reḥem” (the first to emerge from his mother’s womb). The firstborn to a mother who was born via Caesarean section lacks the status of bekhor both with regard to inheritance and with regard to pidyon ha-ben, and therefore need not fast on Erev Pesaḥ (Kaf Ha-ḥayim 470:3; however, some are scrupulous about participating in a siyum).
Firstborn kohanim and Levi’im also observe the custom of fasting. Although the Torah exempts them from pidyon ha-ben, they are nonetheless bekhorim (MB 470:2). A firstborn female need not fast. While it is true that some authorities maintain that female bekhorot also died in Egypt, the prevailing custom is that they do not fast (Rema 470:1, Ḥida, and Ben Ish Ḥai).
Customarily the father of a bekhor, even if he himself is not a bekhor, fasts instead of his son until the boy grows up (Rema 470:2). If the father is also a bekhor, his own fast counts for his son, and if he participates in a se’udat mitzva, he is exempt from fasting for his son as well.
When Pesaḥ falls on Shabbat, the custom is to move Ta’anit Bekhorot up two days, to Thursday.