There is no easy answer to this question without you having a firm knowledge of theory, and lots of practice, BUT, if you just want to noodle around this is what you could try.
First find the key.
Here are the chord qualities for any of the major keys, using the key of G as an example.
G Am Bm C D Em F♯
I ii iii IV V vi vii
Major Minor Minor Major Major Minor Diminished
(Root/Tonic)
So let’s pick the key of G (1 Sharp), as shown above.
1 - Most songs will start and end on the I chord - But not always.
2 - Now just play through the song and plug in the appropriate chord that sounds right to you.
3 - Most of the time you can do this just using the I IV V and vi chords - But not always.
In the key of G this would be the G C D and Em chords. Note: Forget the vii chord for now.
If your question pertains to hearing a song, or playing along to a song someone suggests that you do not know, this will only come with years of practice, and to some this will never happen.
This is called playing by ear. My father was great at this and if someone said play Yankee Doodle Dandy he could do it right away.
If you are looking for a formula for this FORGET IT. ONE DOES NOT EXIST.
The closest to any formula for breaking down a song, and knowing what chords to play, would be the 12 bar blues, because it has a very structured format.
Not sure if this helps you but these are my thoughts on this.