Is it the whole lamp that is loose in the car? If so, find and tighten the mountings. But that is simple, so probably not the answer.
The inner lamp housing is usually sat on a peg and two screws, allowing it to pivot for adjustment. If it pops off the peg, like a ball and socket similar to the interior mirror, but smaller, it can be a real pig to put back on. Ideally you need the front lens off so that you can push. Can sometimes be done with fingers in through the bulb hole and pull, but care is needed to not break or distort the reflector and not to leave dirty fingermarks on the reflector. Clean cotton gloves are recommended, but not easily obtained. Best done with the lamp off the car, this will help see what you are doing, get the pull in the right direction and avoid breaking it.
You've not said what model it is so can't be more specific.