Each show will have set blocks for ad breaks. The breaks will be of a certain time – e.g. three or five minutes. The ads are usually 30 or 60 seconds long (sometimes 15), so will fit into the ad break block.
For TV shows there is usually a fade in/fade out allowing you to trim to exactly the second for ad breaks, movies will tend to be shown with a “stinger”, the logo of the channel broadcasting generally, or sometimes a title card of the movie.
The shows and ad breaks are all timed by a computer. Sometimes for live sports coverage there will still be people manually throwing to a commercial break, but in general it is all automated now.