You cut a circle of fabric larger than the button and hand stitch a running stitch around the edge with the tails of thread cut longer. I would use a strong buttonhole thread. If you have ever made a yo yo it is exactly the same.
You can but not necessary use a little batting or stuffing on top of the button. Might want to glue it down so it doesn't shift.
It would work best for a hat if you use a button with a shank I think.
Place the button on the center of the fabric top side down. Pull threads tight and tie off.
I know they have covered button tools at the fabric store but I have never used them before.
You can buy the metal buttons and I think it would show you how to do it on the back of the packaging.