Why do people with type O blood have anti-A and anti-B antibodies?
To understand what's happening here, let's have a look at the actual ABH (or ABO) antigens:

As you can see, they are not very big molecules: quite the opposite, they are relatively small globosides.
That being said, how it's possible a type O individual producing anti-A and anti-B antibodies without having any contact with the antigens? And that is the same as asking &quot;how it's possible a type A individual producing anti-B antibodies without having any contact with the antigen?&quot;
The answer is: there was contact with the antigens, even without a blood transfusion.
According to Laura Dean (Blood Groups and Red Cell Antigens, chapter 5):

ABO antibodies in the serum are formed naturally. Their production is stimulated when the immune system encounters the &quot;missing&quot; ABO blood group antigens in foods or in micro-organisms. This happens at an early age because sugars that are identical to, or very similar to, the ABO blood group antigens are found throughout nature.

Thus, from an early age, we have contact with the antigens, coming from food or bacteria in our digestive system. As we get in contacts with antigens A and B, this is what happens:

People with type A blood: they will produce anti-B antibodies.
People with type B blood: they will produce anti-A antibodies.
People with type O blood: they will produce anti-B and anti-B antibodies.
People with type AB blood: they won't produce antibodies (regarding A and B antigens).

Source: Dean, L. (2005). Blood groups and red cell antigens. 1st ed. Bethesda, Md.: NCBI.