If I contribute code to a project that is under the GPL (or similar), can I still use my code (without any other parts that were contributed by other people) in a closed-source app? If yes, can I also allow others to use my code without publishing the source code?
It depends. Did you sign a Contributors License Agreement? These agreements usually clarify if the contributor keeps their copyright and just licenses the code to the project or if the contributor transfers copyright. In the first case, the contributor is usually still free to do whatever they like with the code because they still own it (except for revoking the license, of course). In the latter case, the project now owns the copyright and can usually deny the original author to use it. Projects often prefer the latter variant, not because they want to screw their contributors but because it allows them to change the license terms later without having to ask every single contributor to agree.
Without a CLA? It appears to me that the general consensus among the open source community seems to be that no license transfer took place and the original author still owns their copyright. Contributors license their work to the open source project under an open source license ("inbound = outbound"). But should the situation go to court it's kind of a word-against-word situation if a copyright transfer took place or not. I am not a lawyer, so I can't tell how a court would rule under what circumstances in your jurisdiction. It might be important though if you did your work for free (as implied by "donated") or received compensation. In the latter case it might get interpreted as work-for-hire which often includes automatic copyright transfer.