Image>Mode>Indexed color...
It'll then prompt you that it'll flatten all the layers, and then present you with this box:

Make sure there's the limit of 256 unique colours set in there and for the Palette is Local (Selective)
I added a rainbow mix of colours to show you the sort of results you'll get if you have too many colours, if you're not happy with how it looks when the colour is limited try messing on with the Dither options.