Onions can be found in two ways, Much more rarely you'll stumble upon thriving Pikmin colonies, which you can hardly do anything with, but in other cases, you'll find lonely onions lying dormant in the ground, mostly surrounded by a hazard or trapped by something.

Onions become dormant when they have little energy left, entering a sort of hibernation, as such by feeding them sugar they'll become active again, and you'll gain their trust effectively taming them, the onion will start flying and will follow you, 
If you find a dormant onion and don't have sugar to tame it, just interact with it to take it as a non-stackable item, You can place it or drop it to turn it into an entity again anywhere, and then tame it there.

Once you tame an onion, take it to somewhere you consider convenient to have it and sit it, If is the first time it sits after being tamed it will produce a single Pikmin seed you can use to start reproducing them, just drop pellets, crystalized nectar, another dormant onion of the same color, or bring a warden next to the onion while sitting, it will absorb them and produces seeds depending of the absorbed object.
You can always make the onion take flight again and move it after tamed, just remember to sit it again to produce more Pikmin.