Let's start with the easiest first. In the last example, Sam gave John and me a book, the pronoun me takes the dative case, meaning that an action is given to someone or something. We use me for the objective case as well: Sam took John and me to the bookstore. Again the subject is Sam, not me. If all this is too damn complicated, I have a tip: take out the unnecessary character and see what makes sense. Sam took I to the store or Sam took me to the store. This trick also works with the third example. John and I went to the shop becomes I went to the shop.
Who killed Cock Robin? It is I. In this example, it is is a transitive, or linking, verb. No action occurs, and an old rule is that subject pronouns follow these verbs (I, he, we). This also applies to the negative answer: not I. Of course, in modern colloquial usage, few speak like this, and you'd be better off being wrong and fitting in than being right and sounding like a Shakespearean nobleman: Who ate my waffles? Not me. Wasn't me. T'was I, Lord of Grammar and Judgment.
For one final example, I will reference Mary Norris from The New Yorker magazine. Let's take the following example: John is a better athlete than me. A lot of people would argue that it should read, John is a better athlete than I, with a silent am hidden at the end. I would be one of those people. But Mary Norris instead argues that the me is correct because than works as a preposition, not as a conjunction, and since a preposition takes the objective case, me is correct. You can watch her argument here.