How do departments determine the optimal quantity of elective offerings?
Gradually converge to a number that works well, and adapt as things change, by adjusting upward or downward each semester if there were too few/too many the previous semester. 

Most classes, including electives, are on a fixed schedule (e.g. "this class is taught in the Spring semester every other year") that is determined and then adjusted by the method described above. 

Demand is not spread evenly across all electives; a course that is consistently underenrolled when offered every year might be moved to every two years, while a course that is overenrolled when offered once a year might change to every semester.

Demand does not usually change dramatically from one year to the next, so this works pretty well.