Typically bookies don’t take commission as such, there is a spread between the odds they offer such that if they manage to get equal (or nearly equal) action across the competitors, they will always make a profit. This is known as their margin, overround, or vigorish (vig for short). All the bookie has to do is ensure their total odds, in percentage form, add up to more than 100%. For example, in a two-horse race (and ignoring the possibility of any ties), if the odds of both horses were evens, on average the bookie would make no money as whatever they took on one horse, they would pay out on the other. This is because odds of even money, expressed in perecentage terms, is 50%, so with two sets of odds at 50%, they add up to exactly 100%. In practice, in such a situation the odds might be (say) 4/5 on both horses (i.e. you bet $50 to win $40 plus your stake back, i.e. both horses are ‘odds on’). This would translate to percentage odds of about 55% + 55%, leading to a total of 110%, i.e. a margin/overround/vig of 10% in favour of the bookmaker.
Anyone can work out the mathematics of this fairly easily. The skill of the bookmaker lies in setting the initial odds correctly, such that he attracts bets amounting to roughly equal levels of risk on all outcomes, thus ensuring his profit is close to whatever overround he wishes to achieve. Sticking with the above example, if the two horses are genuinely evenly matched, this should work. But if one is in fact more fancied than the other, it will attract more bets and could result in the bookie paying out more than he takes in.
Now, as Teuton has explained, the bookmaker will adjust for this by shortening the odds of a horse that has attracted more bets, and lengthening the odds of unfancied horses to attract more bets, thus balancing their book. But since this is a reactive process, they can still lose out if their initial odds are wrong. Generally, the favourite winning is a bad outcome for bookmakers, since although they are paying out winners at lower odds, they are also (pretty much by definition) paying out many more of them than if an outsider had won at long odds.
I don’t really know why I bothered typing all this since I had to refer to this Wiki article to get it right, and it probably explains it better than I have anyway: https://en.wikipedia.org/wiki/Mathematics_of_bookmaking