This is a strange but common expression. It means that you can't have everything go your way and sometimes you must make a choice. If you eat a cake, you will no longer have it (it will be gone), so you can't both have a cake and eat it. You must choose between keeping the cake or eating it. It is usually stated:
"You can't have your cake and eat it too."