The 27-year-old centre-half, who joined the Bees from non-league Tonbridge Angels in 2009, moved to Kent on loan at the beginning of January.
That initial deal expired after Gillingham's win over Northampton on Tuesday night but Legge has now made the switch permanent.
Brentford manager Uwe Rosler told the club website: "I'd like to thank Leon for his contribution and his professionalism since he joined the club.
"I have a lot of respect for players that come into the game via non-league football, rather than the Academy system.
"He was very dedicated at Brentford and developed as a player but I couldn't give him his wish to play regular first-team football.
"I felt he was still an important member of the squad but I think it's very important that both parties are happy and we accepted Leon's desire to join Gillingham and wish him all the best for the future."
- Sports & Recreation
- Tonbridge Angels