Not every Buddhist cannot take onion and garlic. More precisely, it is those who have taken the Bodhisattva precepts or are vegetarians that do not eat onion and garlic.
The Mahayana Brahmajala Sutra, which sets up the Chinese Bodhisattva precepts, forbids eating meat, garlic and onions.
http://www.sinc.sunysb.edu/Clubs/buddhism/bns/bnsframe.htm3. On Eating Meat
A disciple of the Buddha must not deliberately eat meat. He should not eat the flesh of any sentient being. The meat-eater forfeits the seed of Great Compassion, severs the seed of the Buddha Nature and causes animals and transcendental beings to avoid him. Those who do so are guilty of countless offenses. Therefore, Bodhisattvas should not eat the flesh of any sentient beings whatsoever. If instead, he deliberately eats meat, he commits a secondary offense. (43)
4. On Five Pungent Herbs
A disciple of the Buddha should not eat the five pungent herbs -- garlic, chives, leeks, onions, and asafoetida. (44) This is so even if they are added as flavoring to other main dishes. Hence, if he deliberately does so, he commits a secondary offense.
44. Pungent herbs: "They are: leek, onion, garlic, and a few other such herbs such as asafoetida, an ingredient common in curries etc. Eaten raw they are believed to incite people to anger and disputes; eaten cooked they increase one's sexual desire." Buddhist adepts are advised to avoid them, as their consumption tends to disturb the peacefulness of the mind. "According to the [Surangama Sutra], garlic, three kinds of onions, and leeks are the five forbidden pungent roots. 'If eaten raw, they are said to cause irritability of temper, and if eaten cooked, to act as an aphrodisiac; moreover, the breath of the eater, if reading the sutras, will drive away the good spirits.'"
So basically, these 'pungent herbs' causes anger and passion, it is therefore not good for spiritual cultivators.
Another reason is because the smell of garlic and onion invite spirits, while dharma protectors hates them. So if you enter a monastery, please rinse your mouth thoroughly first.