Affiliation on a paper - moving to a place where I actually did the work
The way this is usually handled is that you list University Y as your affiliation, and in the acknowledgements of the paper, include a sentence that says something like:


  Much of this paper was written while the second author was visiting University X; he would like to thank the X Department of Mathematics for their hospitality and support.


If you end up doing significant further work on the paper after you start your job at X (e.g. major revisions in response to peer review), then you could list both universities as your affiliation.