When you battle against a gym that belongs to your team (training), if you win, you get an XP bonus for yourself, and a Prestige (reputation) bonus for the gym. Enough of the latter and the gym levels up. Higher level gyms can hold more Pokémon (one per trainer), which makes it harder for opposing teams to take over the gym. If you lose against your gym, no bonus for either you or the gym. The Pokémon you initially put in the gym stays there until it's bumped out by the gym losing enough Prestige to drop one or more levels (the lowest CP Pokémon drop off the lower end of the ladder, essentially). The gym loses Prestige by having opposing teams win against it.
If your gym Pokemon is returned to you as a result of the gym losing enough Prestige, your returned Pokemon is returned fainted, with no health, requiring healing via a Revive and Potions before another battle.
In your example, I don't think either of your Pokémon (Charmander left at gym to train, or the Pinsir you bring in to challenge) gain anything (it'd make sense if they did, but I haven't seen it).
Can more than two players simultaneously battle a gym?
Yes, but not in the double battle style seen in the Gameboy/DS games. Both are battling in separate battles to reduce the prestige of the gym and thus take it down.
As @Teaganator describes in his/her comment below: "You can be in the same battle instance with a teammate battling a gym. How that works it both players take damage as if they were alone, but they both deal damage at the same time."
You can also tell if you are battling with someone else because an icon will appear on the left side of the screen under your Pokemon's health that will tell you the nu
Can gym members of X and Y fight together against gym Z?
I would assume so, only because of the way I understand battling a gym as explained above. Both are trying to reduce the prestige of the gym.
Do players teaming up together have to start the battle within a window of
time or simply as long as a battle is in session?
No need to start at the same time because you aren't battling together as a unit. Just attacking the gym simultaneously.
I don't know for sure, but from what I witnesses last night, as long as the second(or subsequent) person joins while the first(or previous) is battling, they will be grouped into the same battle. Now I'm not sure if starting at the same time will cause a problem and create 2 "instances" of the battle. I would hope/assume not though.
Can you team battle at your own gym with players of the same color?
I'm going to assume instead of battle you mean train. In which case yes, multiple members of the same team can train on a allied gym at the same time.
This I do not know. I didn't test this last night. I encourage those in the comments to chime in and I can edit this part.
In the event that you and an ally remove the prestige from an opposing
faction's gym, what dictates who places a pokemon there?
Whoever gets their Pokemon in first. If you're playing with a friend or someone on your team you might want to coordinate what you're putting in. If you think you and a mutual enemy took down the gym then you want to get a Pokemon in there ASAP to claim it.
Edit Note: Last night after defeating an enemy gym, 3 players put in 3 Pokemon immediately after taking down the gym. I wasn't aware you could do this. I thought you had to put in one, increase prestige, put in another, etc. Was this a glitch? And if not could someone explain this to me?
In Conclusion:
There is no special condition when battling as a team. You are concentrating your efforts with another player to reduce the prestige of an enemy gym or raise the prestige of an allied gym faster.
Also I was REALLY wrong initially.
Also, thanks to users Theyna and Teaganator in the comments for being right when I was wrong.
Best Answer
Here is the relevant portion of Niantic's Ditto announcement: