This question has troubled me for ages despite my several attempts of looking it up in dictionaries or usage books. Do we say, "Do you have any ideas" or "Do you have any idea"? I do see an example where "any" means "it doesn't matter who/which/what", therefore "You can borrow any book you like." Also, does it matter between using it in questions and negatives?
It depends on the context. If a group of people is brainstorming (that is, trying to come up with a bunch of creative possibilities to solve a problem), then I might ask someone "Do you have any ideas?" In this situation, I am expecting that they may have several ideas that are relevant to the problem.
However, if I am asking for an answer to a specific question, I might say something like, "I can't remember what the capital of Pakistan is, do you have any idea?" In this situation, I am expecting that they might have one specific piece of information to offer. This is probably a more colloquial or idiomatic expression than the other one.
And yes, the use of "any" does have a lot to do with questions and negatives. "Any" is a negative polarity item, which means it can only happen in certain contexts. For example, I would never say "I have any ideas" or "I have any books", but I would say "I don't have any ideas" and "I don't have any books". I don't really know how to give you a full explanation of where or why "any" can be used though. The example you give about borrowing "any book" is certainly correct, even though it isn't negative or a question.
I've been teaching English for almost five years now. My students have had this question before.
According to the books I've used (specifically Smart Choice by Oxford University Press and English in Mind by Cambridge), "any" is used only for uncountable nouns and plurals and when the sentence is a question or a negative. In the example above about "Do you have any ideas? / Do you have any idea?" consider that "Do you have any idea?" is using idea as a synonym of notion which in turn is uncountable. You would never use "any" for a singular noun you can count. Could you say "Do you have any books? / Do you have any book?"?
Remember that formulas exist in languages but we humans can break them, like the "adjective before a noun" formula in "it came upon a midnight clear" or "All remaining passengers must wait in the lounge / All passengers remaining must wait in the lounge."
In college I was taught that languages evolve because the people who use the language bend the rules. We invent the language, therefore we can re-invent it and reverse the rules at whim.
It may be worth pointing out that both
Do you have any ideas? and
Do you have any idea? are potentially correct, though they have very different meanings and the latter would normally only be used in a very specific context, e.g. rhetorically, as in:
Do you have any idea how much trouble you’ve caused? Do you have any idea?