I didn't know much about that until Suffix brought it up. It's simple--you fight a lot of the same thing over and over, and the stat or stats it represents goes up. Everything has specific EVs to give. For speed, beat a lot of Zubat and Golbat. For attack power, beat a lot of Carvanha.
There's a limit of 510 total EVs with any individual one capping at 255, so don't bother with stats you won't need. Say, I wanted a Politoed with Surf, Psychic, Ice Beam and Rain Dance. I would completely avoid having it fight things that yield attack EVs because the stat is utterly useless. That way, its other stats could be higher. And... I do in fact have a pretty durable Politoed like that.
IVs I still don't know, but I think it's something you can't really control.