"I gave him + INDIRECT OBJECT" vs. "I gave + INDIRECT OBJECT+ to him"
The sentences are both grammatical, and mean the same thing. The difference between them is precisely that they are the two variants of the English Dative Alternation, also known as Dative Movement, or simply Dative.
Most though not all bitransitive verbs (verbs that can have both a direct object and an indirect object, like give) participate in the Dative Alternation, with two possible constructions:
- An Object + Preposition Phrase construction: Bill gave a pencil to her.
- A Double Object construction: Bill gave her a pencil.
This is already problematic in traditional school grammar, where there is widespread dissent about which one of the two NPs in each construction is really an Indirect Object and which one is a Direct Object. Clearly, in terms of form, construction (1) has an object of a preposition but no latinate "indirect object", while the order is reversed and the preposition is missing in construction (2).
The solution is to note that the constructions assign the same roles to the same NPs, so that, in both cases, there is a Source NP (Bill), a Receiver NP (her), and a Trajector NP (a pencil). Source and receiver are clear enough; a trajector is something that moves. So the subject in a dative construction of either sort is the Source, the direct object is the Trajector, and the indirect object is the Receiver. Normally source and receiver are human NPs, while the trajector is non-human.
There are complexities; either object can be passivized, for instance:
- A pencil was given (to) her by Bill. (to is optional in this construction)
- She was given a pencil by Bill. (to is not allowed in this construction)
and they interact in complex ways with the movable particles of phrasal verbs:
- Bill passed out the pencils to them.
- Bill passed the pencils out to them.
- *Bill passed the pencils to them out.
- *Bill passed out them the pencils.
- Bill passed them out the pencils.
- *Bill passed them the pencils out.
(Passivization of these sentences is left as an exercise for the reader :)
I gave him a pencil is the normal choice. The alternative is less likely, and, as you suggest, may be used to give a different emphasis, but whether or not it is appropriate will depend on the context.
I don't think anybody would say "I gave a pencil to him".
We would reserve this arrangement of the phrase for something like: "I gave a pencil to the man sitting next to me", where the indirect object is a long phrase, or in a sentence such as: "I gave it to him", where the direct object is short.