Let me take a controversial stance: the tonic is always subjective.
There is no such thing as one objective tonic at any particular point in a compostition. I'm not arguing that the idea of a tonic is useless, though, I'm just saying that the best conceptual understanding of a tonic (or tonal center, or key center, or a bunch of other phrases that refer to the same central measure of a piece) must recognise that this center is based on perception and is subjective and personal.
A tonal center is like a reference point in physics: yes, you could calculate the motion of that baseball with respect to the wave going around the bleachers, but you'll find it much more useful to analyse the motion of that baseball relative to home plate. In the same vein, a key is a perspective from which analysis can be constructed. Take "Fly Me to the Moon", for example. You could say that the key is C major, or you could say that the key is A minor. Both of these lenses allow one to easily observe patterns present in the song and interpret them. However, it is also possible to analyse the song as being in the B Locrian mode, though of course it is not useful to analyse the song from that point of view. You could even analyse it from the perspective of D major, where of course the first note wouldn't be in the diatonic key and the first chord would be v. Obviously, any theorist worth their salt would throw that conclusion out the window immediately, but that's what good analysis requires: isolating the useful frameworks of C major and A minor, and discarding useless approaches like B Locrian and D major.
It might not even be useful to give a piece a single label with respect to a key: the key can change, as anyone who's learned about modulation knows. A piece can even have multiple "valid" interpretations: I know I've written music before where I meant for it to sound like it was in one key, but listeners identified a different key as the tonal center. The classic example of this is when someone wants to write some music in the Locrian mode as a theory exercise, but it ends up sounding like Phrygian or one of the other modes. Plus, music doesn't have to be diatonic; composers have been blurring the lines with regard to the key of a piece for centuries: modal interchange, secondary dominants, chromatic planing, and plenty of other techniques to get off of the white keys and play with the listener's perception of the key. And once you get to polytonalism and twelve-tone serialism, any opposition to my point really starts to fall flat on its face.
This is not just a theoretical semantics debate: there are numerous well-known examples of music that has seen analyses with varying tonics. For some well-known extreme examples, "Giant Steps" - not just one "objective tonic" (not even close). "4:33" - I'd love to hear your opinions on the tonic of that piece. Even "Sweet Home Alabama" has been making the rounds in the media as an ambiguous case.
"So if the tonic is always subjective, then how am I supposed to analyze music?"
, you ask. Well, the tonic is what you personally hear as the note around which everything falls into place, usually best determined by the melody. One good way to check is to see what notes are in the song and then see what key would have those diatonic pitches. Be very careful doing this, though - this method quickly falls apart with music that gets out of diatonic harmony (which probably happens more often than you'd expect). If you hear a change in that basic set of notes at some point in the song, that's a clue that the song may have changed its tonal center. It's also often useful to study cadences and their patterns of resolution, but again, this can be and is often subverted. Ultimately, trust in your own ears first, then check to see whether other clues confirm this hypothesis. Experienced musicians will do this process in their head from multiple angles at once ("hmm, the notes are blah blah and blah, the melody sounds to me like blah, and this makes blah blah type of cadence - pretty clearly in F♯ minor!").
"I think I'm doing this right, but sometimes I get the key 'wrong'. How do I know I'm using the right process to figure out the key?"
If you can do this process correctly, you should have no trouble analyzing the blues. Often, a struggle to determine the key of a blues song is an indicator that someone is using the wrong methods to arrive at their analysis of the key of a piece.
"Wait a minute, how can there be a 'wrong' way to figure out the key? Didn't you just spend a whole paragraph explaining that the key is subjective?"
Okay, technically there's no such thing as the wrong method, in the same way that analysis can't really be "wrong" either. But that doesn't mean that there can't be flawed logic involved. When people talk about "the wrong key", that's a shorthand for the implicit understanding that the key identified by that analysis is useless in terms of understanding the music. In the same way, the "wrong" way to analyze the key of a piece is "wrong" because it often leads to useless ("invalid") conclusions, and is not very efficient for arriving at useful analyses.
If you want to get better at doing this identification, the best and fastest way to improve is to listen to a lot of music (like, a LOT - this comes with experience) and just take a second to try and determine the key. If you can, get another musician to verify what they hear as the tonic - the internet can and often does lead to counterproductive interpretations, because computers are bad at subjective tasks like this one. Plus, the humans posting on the internet are not all experts (hopefully, you trust that I know what I'm talking about, but there are plenty of people willing to post stuff online that don't have a solid grasp on what they're talking about).
Occasionally, you may stumble upon a piece that you just can't seem to wrap your head around - even if you're a seasoned veteran theorist. Those are the cases where it can be best to reject the idea of a key entirely, or use two keys to explain the patterns you hear, or identify a change in the key, and just accept that it's possible to interpret something multiple ways, and that there isn't always just one correct answer.
One final (hopefully) addendum, just as a nod to some other sentiments that I may have initially done a poor job of explaining:
My answer above is more of an advanced conceptual-level approach to harmonic understanding. It works very well for the intermediate-to-expert theorist, but it can be a bit involved for a novice.
Ultimately, your goal in developing the skill of identifying the tonic should be to be so quick and accurate that for most songs it will feel objective. User Albrecht Hügli's answer is a great starting point towards developing this skill from a novice standpoint. You should be able to say to yourself most of the time, "Yep, the song's obviously in D minor" without a doubt in your mind. You of course would know conceptually that it's a shorthand for knowing that D minor is by far the most convenient choice of a key for conventional harmonic analysis. When you practice, especially as a beginner, there should most of the time be one best answer - if there's more than one, you're picking the wrong examples to practice with.
To sum it all up:
Learn to figure out the key of simpler (one clear best key) musical examples efficiently. Once you have that skill in the bag, then in order to tackle more advanced harmony, you must grasp that a tonal center is a perspective from which anaysis makes sense, and learn to trust your ear.