edit: The cake case was due to state laws, though.
> No federal law requires businesses to serve all customers without regard to their sexual orientation, but 21 states have “public accommodations” laws that prohibit discrimination against gays and lesbians. - http://www.latimes.com/politics/la-na-pol-court-gays-religio...
The government equally could adopt a law prohibiting discrimination based on advocacy of race-based violence in public accommodation as several state governments have for gender preference.
> Yet the government can force private entities to sell a cake to a homosexual couple?
Laws reflect human preferences, which are at some level arbitrary. Apparently, many states think that discrimination against gays is a problem that needs to be addressed, whereas discrimination against violent racists is not.
The law will always seem arbitrary to you as long as you are ignorant of it.