Very rarely dogs can have a disease which predisposes them to weight gain such as hypothyroidism (which, by the way, is easily diagnosed and treated) but in general if more goes in than goes out then it stays on as fat – just like us. There is no other way for it to get there!
In very rare cases of obesity that is not responding to diet foods and where no underlying medical reason is found the vet will discuss with you the use of certain medications and supplements which can act to block absorption of fat in the intestine and/or decrease the appetite.