The first equivalent of ammonia acts as a nucleophile, substituting for bromine. The second equivalent acts as the base by deprotonating the amine.
The amine formed, like ammonia, is nucleophilic and tends to react with the haloalkane that remains free in the medium, forming secondary and tertiary amines. This problem makes the method not very useful, due to the final mixture obtained.
The amine formed reacts again with the haloalkane, alkylating itself a second time. This problem is called polyalkylation.