I stopped by the store on the way here and bought groceries. Better safe than sorry, as they say.

Literal

Coming midway, shop [to-に] stopping-by food-ingredients [object-を] bought came [explanation-んです]. Preparedness exists-[if-ば] worry without, [copula-です].

来る途中 ('on the way coming') sets the scene. 寄って…買って来た chains three て-form verbs showing sequence. んです adds the explanatory nuance ('the reason I have groceries is...'). 備えあれば憂いなし is a famous proverb (originally from the Chinese classic 書経): 'if there is preparation, there is no worry' = 'better safe than sorry.' The です at the end politely frames the proverb as the speaker's philosophy.