The California Density Bonus laws override city’s zoning ordinances. They are intended to provide more housing for people of quite moderate means. Applying them is the choice of a developer and they seem to be applied mostly when they increase developer profit.
Here are some good references:
A Developer’s Guide (includes text of the law)
California AB2222 (changes to the law)
It has been claimed that developers in Encinitas have pushed this to its limits. In order to challenge its use Citizens need to be aware of items where local discretion is being abused.