As you've got to make the journey between airports anyway, I say make the most of it.
Sleep on the plane if you can and see the city at night.
I'd pack in one backpack (hand luggage strapped on top) and do it on foot, but taxi/bus/night tube (Friday and Saturday) are other options.
The last tube train from Heathrow is at 23:35 (T4) or 23:42 (T5), a few minutes later for T123.
So you should have plenty of time to clear immigration, retrieve your luggage and get the tube.
There's also the TFL line train
but that goes to Paddington station which is 3 km from Buckingham Palace).
You can check for planned/emergency issues online at Transport for London's website .
If you get the tube, you can go to Hyde Park Corner, less than 1 km away from the Palace.
In the morning I used to quite often get an early train out of London Bridge to Gatwick.
Currently that looks to be 05:35, getting in at 06:04.
That should be plenty early enough for most flights but you'd need to check.
Assuming you're happy to walk through the night you can see a lot of London
Here's a quick 10 km route suggestion I threw together.
It takes in Buckingham Palace, Parliament Square (but not Big Ben/Elizabeth Tower which is being repaired), the Embankment, Trafalgar Square, Covent Garden, the Strand (you could night-bus this bit), St Paul's Cathedral, the Tower of London and Tower Bridge.
Here's a picture in
case I carelessly delete the route Night buses from Trafalgar square will get you to many places but they're not very frequent.
I've deliberately omitted airport buses in the interest of maximising your time in London.
The tube and regional trains aren't all that expensive, and are much quicker; they're also less much affected by traffic, though there are rare delays in the middle of the night.
travel.stackexchange.com/users/18491/Chris H
Yes, it's doable.
Although going through central London isn't the fastest route from Heathrow to Gatwick, you have lots of time
so it's not an unreasonable route to take.
You could obviously do it all by taxi, but it's also possible (and much cheaper) on public transport.
You can get the underground from Heathrow to Green Park, which will take about 50 min, and then you're only a few minutes walk across the park to the palace.
You can then walk to Victoria station, and get a train to Gatwick.
The last train to Gatwick is at 00:08 (though trains from other stations and coaches run through the night, and the ones from Victoria start pretty early in the morning).
travel.stackexchange.com/users/26849/djr
Very easily doable.
But if you're going all that way in on the tube and aren't too tired, walk
around - central London is quite small and easily walkable.
You could see Tower Bridge, Tower of London, Big Ben, Trafalgar Square and Picadilly Circus - all quite photogenic and iconic, even at night!
travel.stackexchange.com/users/101/Mark Mayo
Yes, with Caveats.
Budget for contingency - 2 hours on landing, maybe 3 for T5.
I'm not kidding about T5 at this time of year.
A morning arrival might have been better, it will be dark.
So everything else is down to visibility, you will miss peak hours.
Suggest you get a driver both ways, or tube and cab.
Buses do not pass the Palace.
You are taking your chances with visibility as well.
Plan to be back at the Airport at 06:00, again, for contingency.
Risks:
Snow, Tube Outage, Rain, Freezing Rain, Sleet, Traffic, Holiday events
(Winter Wonderland is close by).
travel.stackexchange.com/users/27539/mckenzm
Per other responses, the short answer is "yes you do" but you're only going to see the outside of it.
A brief stroll back up the mall will land you at Nelson's Column, and you're near enough some night-life such as Leicester Square, or the theatres on the embankment, as well as within wandering distance (or a short tube ride) of the illuminations in Carnaby Street (Bohemian Rhapsody this year!)
and there's doubtless a few others to see too.
I don't know the opening times but
over the river you've got the London Eye which could provide a decent photo op too.
Just a shame Big Ben is scaffolded up for repairs for the forseeable.
travel.stackexchange.com/users/20361/John U
It takes 45 minutes to get out of the airport and another hour to get from Heathrow to BP.
You'll arrive at BP at 10.15pm.
Not much to see at there at that time, so I think you'd do better to head over to Waterloo Bridge and then wander up in to Covent Garden, Leicester Square, Picaddilly Circus, and Soho.
The cheapest way to get to Gatwick is by coach from Victoria Coach Station.
It's a miserable trip, early in the morning, but it sure is cheap.
travel.stackexchange.com/users/52179/Strawberry
My plane lands at Heathrow at 8:30 pm on December 10 and leaves Gatwick the next morning, December 11, at 9:15 am. Do I have the time to see Buckingham Palace?
Is a 13-hour layover in the middle of the night enough time to go see Buckingham Palace?
travel.stackexchange.com
<uk><layovers><london><sightseeing>
travel.stackexchange.com/questions/126303
travel.stackexchange.com/users/87378/Charmaine
This is very doable, although you will only see the outside. You could travel by taxi, but it would be cheaper and also possible to use public transport. The  last tube train from Heathrow is at 23:35 (T4) or 23:42 (T5), and a few minutes later for T123.   It is completely possible to get to Buckingham Palace and make it back in time for your flight. There is also a range of transport options you can choose from such as; the Tube, Taxis, Personal Driver etc.
It is completely possible to get to Buckingham Palace and make It back in time for your flight at Gatwick. There are a number of transport options to get to a from both airports at the time of arrival such as; Taxis, Train Tube etc.