Great question Marc,
This is an applied skill with one simple, yet complex prerequisite: You must be able to match a pitch from the song with a note on your keyboard, and you must know the K23 and K122 Major/Minor patterns taught in Lesson 1 of the Melody Course
You can also use an online service such as https://www.audiokeychain.com/ to estimate the BPM and key of your track and while this is not always accurate, it can help you get a head start.
My trial-and-error method:
Step 1: Play a melody from the song you are trying to find the key of, then play each note on an instrument in your DAW while listening carefully.
Step 2: Once you have found a note that matches up with what you are hearing from the song, use that note as the “Key note” and play each note in the Major scale, then the minor scale associated with that key note while listening to the song.
Step 3: It is then up to your ears to be able to tell what notes “work” and what notes don’t work. If any notes from the scale do not sound good, try only playing arpeggios as the song you are listening to may only be using chords from that key.
Step 4: Keep in mind that key changes can be frequent in some music, so just because the notes are working at the first part of a melody do not mean they will work in the second part! Always be ready for a key-change!
If you would like a video on this process, I may be able to dig something up for you!
Thanks again for contributing to classroom discussion, Marc!