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.