I don't think there's any law about it on the ground, at least in most countries.
So in that case it is up to the pilots.
If you're flying in Europe on a short haul carrier the turn around time is pretty tight and the pilots will have a lot to do to get ready for their next sector. Also the plane has to be cleaned and so on in about twenty minutes so really they want you off the aircraft ASAP.
But if you just took a flight from London to San Francisco the pilots have a bit of paper work to finish and then they go to the hotel. So on longhaul flights the flight crew are often happy to show you around the cockpit—especially if you have kids. If you travel on BA the pilots sometimes have a kids' pack to give out to kids that they invite to the cockpit. The pilots sign a kind of "log book" for the kids so they can keep track of all the planes they go on. I'm sure other airlines have similar things.
If you are an adult they will also oblige you as well! A friend of mine was allowed to sit in the captain's seat (after landing) recently on London-JFK, and got a nice photo for his Facebook page.
Best is to mention it to the cabin crew when they don't look busy. A polite request such as, "Hi, I wonder if, after landing, we could have a look in the cockpit?" will be sufficient.
Sometimes you will even be invited to access the cockpit in flight, but this is a bit more rare these days. In that case it depends on a lot of factors including the local law (of the aircraft registration country, country of departure and country of arrival) and how strictly it is applied.