The official rule of a Touchdown is defined in the NFL rulebook.
Touchdown: When the ball, in legal in bounds possesion of a player, crosses the plane of the end zone, provided it is not a touchback.
The ball always must be in a player's in bounds possession and cross the goal line for a Touchdown to be ruled. The ball must cross the goal line regardless of the player's position.
Furthermore, the ball can never be "out of bounds" unless it physically touches the ground itself. In ALL other cases, a part of the player's body must touch out of bounds, regardless of where the ball is. If a player's body crosses the End Zone in bounds, and the ball crosses the End Zone as well, while the ball might be out of the playing field, it's still a touch down because the ball did cross the imaginary line of the EZ.
In the first scenario your described, the play would still be a touchdown because the player was in bounds when the ball crossed the goal line.
The the second scenario, the play should not be ruled a Touchdown because the ball never crosses the plane. I've seen this rule enforced once, where a tightend caught a pass on the goal line, but extended his arms out in front of him. Ray Lewis hit him from behind and stopped him in his tracks. The TE caught the ball, and it was ruled a touchdown, but on further review, an overhead view clearly showed that the ball was a good foot short and it was overturned.
To touch on what some other's have said, the pylon is considered in bounds, so if the ball crosses the plane of the endzone, in a player's possesion, but the player is flying through the air and lands out of bounds, if he hits the pylon, with ANY part of his body OR the ball, it is still a touchdown. This ONLY applies to players who have established legal in bounds possesion.
If a reciever catches the ball in the air, but never lands anywhere in bounds, it is NOT a touchdown, because, by rule, he never established legal in bounds possesion. Even if a part of his body hits the pylon mid-air before landing out of bounds, it isn't a touchdown because legal in bounds possesion was never established.
Then of course, there is the force out rule, which we'll save for another day.