I like listening to classical music, I noticed that a song may come in many versions (example : Hungarian Dances). I know what majors and minors scales are, but my music theory knowledge ends there. I fail to see the link.
Those aren't the same song. Brahms (to take your example) composed entire sets of "Hungarian Dances," simply labeled Hungarian Dance #1, #2, etc. Often these are referred to by the key they are in: thus everyone knows which particular Hungarian Dance you mean when you say Hungarian Dance in F#m. (It's #5, I believe.)
The first bar of Twinkle Twinkle Little Star in the key of C major goes:
C C G G A A G
The tune's "home note" is C. That is, when the melody returns to C (as it does by the end, six bars later), it feels resolved. All of the notes in the tune come from the C major scale.
You can play the same tune in Bb major by moving all the notes down by a tone.
Bb Bb F F G G F
You would recognise it as the same tune, but now the tune is centred on Bb, and all the notes uses come from the Bb major scale. This is called transposing.
If I asked you to hum the 1812 Overture, you would probably instinctively and unknowingly transpose it, because you would pick a starting note arbitrarily and go on from there.
However, Brahms' Hungarian Dance in G minor is not a transposition of Hungarian Dance in D minor. They are two different Hungarian dances.
19th century composers were often not all that imaginative in their naming of pieces (see also Why is the key included in classical music titles?), if they named them at all, so we end up needing to refer to works as, for example, Symphony in C minor simply to distinguish between them.
Certainly you could transpose Hungarian Dance in G minor to D minor -- and for many listeners it would be pretty much the same piece. The change in pitch does have effects, however. See What does it mean to write a song in a certain key?
A piece played in C major is played using the tones of the C major scale. It inherits the tonic, dominant, leading tone, etc. of the scale it is in. A piece in D minor is inherits the same from the D minor scale. Whenever major or minor is not designated, the piece is assumed to be in a major key. The key a piece is in determines what tones are used throughout the piece and what purpose those tones serve structurally.
A Hungarian Dance in G is a different piece from a Hungarian Dance in D. Although it is possible to transpose a piece (change it from one key to another), most recordings and performances are in the original key. These two dances would have a different theme and be fundamentally different pieces which simply happen to have similar names. Two sonatas in A minor could also be different, even though they are both unnamed sonatas in the same key.
'Version' isn't the right word. 'Key' is. When a piece is written, the composer will decide (for various reasons) what the tonal centre, or key will be.The piece will revolve around those notes. The notes in the scale of that key. Be it major or minor. Or even modal - often a mix of major and minor.
The underlying harmonies will also revolve around that key, with the tonic chord usually feeling like 'home', and the dominant mostly leading to it.In Western music, particularly, this is the case.
Pieces are often labelled ' in Bb' etc., to identify them, mainly to the musicians who will play them.It really does sound better if all the musicians are playing in the same key ! For a lot of listeners, if, for example, a Bouree in C was played in D, it would go unnoticed. However, if the original is in C, that's its name.
Having said all this, certain keys do evoke certain feelings for some people, which is why certain pieces are in certain keys.