As you would imagine, they simply stood. The power of a phalanx was in its depth, but this did not necessarily mean those troops were actively doing anything besides trying not to run away. Casualties in the actual combat during ancient battles was surprisingly low, but the series injury came when one side decided that all was lost and broke. The pursiut is where the vast majority of casualties occurred. The role of the men in back of a phalanx was to keep this from happening. The men in the front, most often chosen for how brave they were, were kept form fleeing by the numbers of men behind them (thus greek armies avoided thinning out their phalanx whenever possible).
Sources: Adrian Goldworthy (most of his books), Ian worthington (By the Spear) and Tom Holland (Persian Fire)