Back in the 1980s I did some work on a computerised tabulation system for a large race course: my part was programming EPROMS for the displays, but I learned a lot about the system from that time.
Basically, the house takes their cut from all the bets. The house cannot lose. Then they share whatever's left amongst the winners.
The returns for a $1 bet on a horse are calculated in real time and displayed on the screens. THESE ARE NOT THE FINAL ODDS, they change as bets are placed right up until the race starts, which is the starting price (and where the term SP bookmakers comes from).
Contrast this with bookmakers at the track who offer fixed odds when the bets are placed. They stand a chance of losing if they get their numbers wrong.