You pretty much only have two choices if your headliner fabric has pulled away from it's foam backing. Replace the headliner or live with it. If you're cheap like me, you'll decide to live with it. But, you'll have to find a way to keep it from sagging and off the top of your head. If the headliner is so old that the fabric has separated from the foam, then glues and spray adhesives don't work very well.
After experimenting with glue, what most people do next is revert back to the old standby - thumbtacks and pushpins. Those items present their own challenges because the backer board under the headliner fabric is not conducive to holding straight pushpins and thumbtacks. Often, more pushpins fall out and end up stuck to your backside than in the headliner. OUCH!
What I found to work really well is something that looks like a tack but is slightly different in design. They are called doily pins or twist pins. Their intended use is to keep mattress pads, slipcovers, and doilies in place, but I've found them to work exceptionally well on sagging headliners.
The pin portion looks like a little cork screw. Instead of pushing them into the backing board, you twist them in. They are inexpensive, and I've never seen one come out on it's own. They are available at most stores that sell fabrics and craft supplies.
Tip: The heads tend to crack and twist easily on some of the imported pins. Shop around for best quality.