It's important to understand that it is impossible to have all of your stats maxed out. The total amount of EVs that a Pokemon can possess is 510 EVs among every stat, with a maximum of 252 EVs in a single stat.
Furthermore, each Pokemon species gives a set number of EVs in every stat when defeated. This is called an EV yield. For example, Rattata has an EV yield of 1 Speed EV. If you defeat a Rattata (whether it's a wild or a Trainer Pokemon), then you will receive 1 Speed EV. Thus if you defeat 252 Rattata, you will eventually have 252 EVs in your Pokemon's Speed stat. If your Pokemon has 31 Speed IVs, 252 Speed EVs and a beneficial nature for the Speed stat, then it should have the maximum Speed stat for that Pokemon species.
By defeating the Elite Four constantly, you will be giving your Pokemon random EVs. To max out your Pokemon's EVs in particular stats, you would have to defeat the Pokemon that will give you EVs in your desired stats. Every Pokemon species has a different EV yield and you can see all Pokemon EV yields on this list.
Of course, the task of defeating 252 Rattata is a very tedious process. This process can be sped up with the use of EV-enhancing items, such as the Power Anklet. In Sun and Moon, Pokemon holding a Power Anklet will gain 8 Speed EVs from every Pokemon it defeats! So when you add that to Rattata's usual EV yield, you will receive 9 EVs from every Rattata you defeat! That means you only need to defeat 28 Rattata to get 252 EVs in your Pokemon's speed stat. Furthermore, PokeRus will double the EVs received from the Pokemon's EV yield and the Pokemon's EV-enhancing item. So if your Pokemon defeats a Rattata while it is infected with PokeRus and holding a Power Anklet, it will get 18 EVs from every Rattata it defeats!
If you want to learn more, try looking at the many EV training guides scattered across the internet. They are sure to help you. Good luck!
Hope I helped. :)
Source and above link