First off all... If you're new to this, relax. There's a few sensible precautions to take that make a difference - sanitise your equipment before hand, keep it upside down when empty, keep your brew covered when not actively using it, start with either a bought yeast / bug, or a starter from a friend / trusted source. Do all these and you'll almost certainly get a decent brew. Almost everything else you read affects either speed of brew, the type of beer/wine /etc., or the nuance of flavour.
Time this depends on what you're trying to make. If you want a small regular amount of tonic or health drink (similar to the way kefir is made and used), then a pint of brew with a little sugar could be ready in 2 days. This kind might be fine (depending on heat / yeast type / volume /sugar concentration) for up to 2 weeks before you would need to drain and refresh the fluid and add sugar to 'feed' it. If you want 3-5% beer for drinking, then 1.5kg sugar in a 2 gallon container at 15°ish might take 2-4 weeks. I used to make 5 gallon batches of ginger beer with 3 or 4 kg sugar and champagne yeast that I left for 6 or 7 weeks before bottling and it wasn't always finished fermenting by that point.
Vinegar(and related sour & off flavours) can basically be caused by 4 things. In order of most likely /fastest...
A **Bad /unwanted bacteria and yeasts. These float around in the air, particularly near any kind of non - alcoholic fermentation and microbe growth (eg bread making, kefir, vinegar making, kombucha, and also smelly bins and grimy or mouldy kitchen corners). This one is the reason for taking care about sanitation. No need to go mad about sanitation, but take care like described above. The reason they're unwanted is because they don't just produce alcohol, they also produce vinegars and other things that sometimes taste off.
B ** Brewers yeast / ginger bug eating the sugars in the presence of oxygen. Your starter yeast will produce co2 which will gradually push all other air out of the way in a sealed /one way container. This co 2 release is why cheese cloth works up to some limit of time (see below), airlocks work for longer and a sealed & released container or a fancier pressure released container are basically fine for a long as you like.
C ** Oxygen directly reacting with alcohol = vinegar. With a cheese cloth covered container, oxygen will gradually start to enter the container a bit of time after the fermentation has stopped. This happens slower without live yeast to help it along. This is NOT a problem if you have a sealed container or pressure system or airlock as these will all keep some co2 pressure inside the container after the fermentation has stopped. If you're doing a 1 pint tonic type thing with cheese cloth then I'd probably put it in the fridge after 3 or 5 days so that the bug slowed right down but kept producing a little co2.
D ** Dead yeast can sometimes impart a small flavour to the finished brew. This is the other reason for A. Essentially, brewers yeast or a good home grown bug are chosen 1) because they mostly produce alcohol not other stuff, 2) because they don't taste too bad in themselves, and 3) because they sink or float, making it easier to separate the finished beer from the yeast. They'll only taste bad if an unwanted stain of bacteria or yeast has gotten in and started eating either the sugar or the dead yeast. Otherwise you're fine.