If I am going through security from Toronto to New York, are the security agents there Canadian or American? Are they working for Canada or the US? Do they need some application/document if they're working for another country?
The security agents (the ones who scan your luggage and body pre-boarding) are Canadians and work for Canadian Air Transport Security Authority (CATSA). I'm sure they cooperate with the relevant U.S. authorities as required.
The pre-clearance folks (U.S. destinations only) are Americans and work for the Customs and Border Protection (CBP) agency of Homeland Security. They have limited powers while on Canadian soil and are unarmed, but can still ruin your day if so inclined.