When you give your spies the assignment to Counter Espionage, what you're doing is telling them to foil plots far and wide. If possible, not only will the plot be foiled, but the opposing spies will be captured and imprisoned. It's also possible that they might be killed in the process, but spying is a dangerous game.
Whichever planet your spies are on when you assign them to Counter Espionage will be the one that holds those prisoners. Once they are imprisoned you can view the list of prisoners at the Security Ministry.
If you have prisoners then you should probably put at least one spy on gathering intelligence, so that they can interrogate the prisoners and learn important details about your enemies.
It should be noted that if you have a spy on someone's planet, and you put your spy on Counter Espionage, you're going to help them defend their planet, perhaps even against your own spies if you also have spies on the planet conducting other operations.
This assignment may only be run on your own or allied planets.