All perfect praise be to Allah, The Lord of the Worlds. I testify that none is worthy of worship except Allah and that Muhammad is His slave and messenger.
What is prohibited during the prayer is to spit in the direction of the Qiblah or to the right-hand side. The justification that was given about the prohibition of spitting in the direction of the Qiblah is due to one conversing with Allah (during the prayer), and to the right-hand side due to the respect to the angels. Nonetheless, it is permissible to spit to the left-hand side, or to the floor.
Abu Hurayrah narrated that the Prophet said: “If anyone of you stands to perform the prayer, he should not spit in front of him because he is silently conversing with Allah during his prayer, and he should not spit to his right-hand side as there is an angel on his right-hand side; rather, he should spit to his left-hand side or under his feet so that he would scrape it off with his feet [bury it]." [Al-Bukhaari]
If one was forced to spit (overcome with spit) while he is in the mosque, then he is forbidden to spit in it. Rather, he may spit in a handkerchief and the like, if possible, or on the left-hand side edge of his garment, as stated by An-Nawawi in his book Al-Majmoo'.
Holding the spit in your hand is dirty and unclean and repulsive. It is better to spit on a handkerchief or directly on the garment so as not to contaminate the hand. Contaminating the hand leads to contaminating whatever it touches of the carpet of the mosque.
For more benefit, please refer to Fatwa 90703.
Allah Knows best.