The key will not be able to be removed unless the trans is shifted into park. If it doesn't see that it's in park then it won't let you remove the key. The next time this happens try moving it in and out of park to see if that helps. If it does it could be either an adjustment is needed or replacement of a bad switch to repair the issue.
Actually I have had this problem for several years with my Buick Regal. I spend quite a bit of money to have it "fixed" 3 times and finally gave up. I found if I waited 5 minutes and then started the car again, the key would come out. However, today it had been two hours parked in my driveway and it still wouldn't come out. So I saw on another internet page two solutions: 1) Underneath the ignition switch on the steering column, there is a plug the size of a dime. Pry it off with a screwdriver. Then put the screwdriver in the hole and your key will come out. YEAH!!! I will always keep a screwdriver in my Buick now. 2) Supposedly for a longer term solution, spray the gear shifts with WD40. I think it has to be done fairly often because I have had it done before. But apparently it helps.
I had and fixed this problem. Remove trim covering center console by pulling up on it. There are push-in clips that are made to simply pop out. Then find the contact switch way down that moves when you push the button on the shifter with your foot on the brake. You may or may not need to remove something else I can't remember, but once you see it thats what you need to clean and lubricate.