You're talking about sentences where the verb has a direct object and a preposition followed by an indirect object. Sentences of the type:
'subject' +'verb' + 'direct object' + preposition + 'indirect object'
In the sentence "I brought the apples to John" , "John" is the indirect object and "the apples" is the direct object. The indirect object here is preceded by preposition "to".
Another example would be: "I bought the box for Kim". Again here "box" is the direct and "Kim" the indirect object. The preposition is "for".
In these examples it is often possible to change the order of the objects and drop the preposition before the indirect object.
That is, instead of:
'subject' +'verb' + 'direct object' + preposition + 'indirect object',
we can have:
'subject' + 'verb' + 'indirect object' + 'direct object'.
Some verbs that allow this are: bring, give, hand ,leave, offer etc (these take preposition "to") and book, build, buy, make(these are "for" verbs).
Some cases when this is possible:
1. When the indirect object is a pronoun or a noun and the direct object is a noun or phrase.
** "I brought Mary the apples"** or "I brought them the books they wanted".
This makes sentences like the last one where the direct object is a phrase or clause easier to understand and less clumsy.
2. It is not possible when the direct object is a pronoun such as "them and it" and the indirect object is not a pronoun and the preposition is "to".
Apparently this restriction is only when the direct object is "it" or "them", and not when it is any other pronoun(Eg: some, one etc).
3. It may be possible to drop the preposition when the direct object is "it /them" and the indirect object is also a pronoun. You might say "Bring me it".
4. You can't drop preposition when the direct object is "it or them" and preposition is "for". That means: "Buy me the apples" and not "Buy me them".
So to look at your examples:
The intended meaning is "Give it to me". By 3 above the rearrangement is possible but not common. It is better to stick with "Give it to me" than go with "Give me it". You could not say "Give John them". Though the meaning is clear, the construction sticks out as odd.
"Buy me them" is not correct by 4 above. You should say "Buy it for them".
"Give John it" is wrong,by 2 above. You should say "Give it to John".
"Buy John them" is wrong by 4 above. You should say "Buy them for John".
In all these above examples, when the direct object is anything other than "it/them" you can readily drop the preposition and place indirect object first.
Eg: Give me one., Buy me some., Give John a few, etc.
Reference:
1. http://www.amazon.ca/A-Practical-English-Grammar-Paperback/dp/0194313425