In order to answer this question, you have to know where cavities come from.  Teeth are made of minerals, and cavities are caused by the teeth being demineralized.  Think of an egg.  It has this strong outer shell, but if you leave the egg in Coke overnight, what happens?  The shell gets soft and eventually breaks down.  This is what happens to your teeth.  Anytime you eat anything sugary or starchy, it feeds the bacteria in your mouth.  When the bacteria eat, they produce acid.  This acid causes the pH in your mouth to drop creating an environment similar to the egg in Coke.  Over time, the outer layer of your tooth softens, it becomes even more susceptible to this pH drop, and this is how cavities are formed.  
So how do you stop it?  I like to think of it as a game.  The object of the game is to get your mouth back to normal pH.  Now, it isn’t always convenient to run off and brush your teeth every time you eat, but there are a lot of little things that you can do.  The first thing is the easiest and what I always tell my high risk patients to try first.  DON’T sip on anything besides water all day.  When you sip on anything besides water, you are bathing your teeth constantly in something that is hurting them.  So, drink the Coke, drink your coffee with all the sugar you want, but do it in a short period of time and then let your teeth rest from the assault and get back to normal.  If at all possible, drink some water after your sugar drink to get the process started quicker.  You can also chew on a sugar free gum.  Some of them, like Orbit, contain Zylitol that actually fights cavities.  
And what about brushing?  Brushing is a double punch!  The actual process of brushing helps remove the plaque and bacteria that causes the pH drop that causes the cavities!  The more plaque and bacteria you remove, the better off you are.  In addition, if you choose to use fluoride, fluoride actually remineralizes the teeth.  So fluoride actually makes your teeth more resistant to cavities.  
However, diet isn’t the only way to get cavities.  I would be remiss if I didn’t discuss how most of my patients got cavities.  My average patient age for the last 15 years has been over 60.  This population doesn’t get cavities the same way younger people do.  Most of the time, they get cavities from the effects of their medications.  Saliva is one of the biggest protectors of teeth.  It is a natural buffer for the teeth from the acid that destroys them.  Many medications have a side effect of decreased saliva.  The more medications you are on, the more this side effect is compounded.  Often physicians don’t discuss the effect that lack of saliva can have on your teeth.  I can’t even tell you the number of times where I have seen a great, healthy patient come back with many cavities after just 6 months when they start a new medication.  It is always so frustrating for the patient who wasn’t told the risk.  This is why it is super important to update your dentist when you start a new medication so that we can help you combat the effects on your teeth.  
So, yes, your dentist would love for you to brush your teeth at least twice a day.  But, even if you do, we can’t guarantee you won’t get cavities.  It is important to protect your teeth from too much sugar and also to be aware that your medications can cause major dental problems.  As always, if you have any questions, please reach out!