The point of advertising on television is to reach the greatest number of potential buyers. Some time slots cost more because they are more likely to reach a larger number of buyers. Prime time television reaches more people than television in the middle of the afternoon when most people are at work and children are at school. For this reason, prime time slots will cost a lot more.
Advertisers are willing to pay more for these slots because they bring more value to the advertisers. They reach more viewers and therefore are more effective than ads in other time slots.