There is a mythological story behind this. During samundra manthan, when Vishnu who had taken form of Mohini was distributing nectar to the demigods a demon (Rahu) also came there. Thinking him to be a demigod, Lord Vishnu gave him some nectar. When the Sun and Moon demigods told Lord Vishnu that he is a demon, Lord Vishnu immediately beheaded him. But since Rahu the demon, had already taken the nectar in his mouth he became immortal.
When Lord Vishnu beheaded Rahu, few drops of nectar fell on the ground from which ONION and GARLIC grew. Since these two vegetables were grown from nectar they were capable of curing diseases and killing germs and since the nectar is dropped from a demon's mouth these vegetables have a strong odour and they are inauspicious to be offered to the Lord. Also whoever consumes them their bodies become strong as the demons but mind becomes polluted and develop negative thoughts.
Whoever consumes onion and garlic their mind and body develop mode of ignorance. One is not able to concentrate spiritually. That is the reason why scriptures don't allow consumption of onion and garlic.