initially they learn that they do the thing asked of them they get a treat. With mine i gave treats for everytime he sat, stayed, or lie down for quite a while then i would do it with a treat every 4 in 10 say then less or sometimes more. He never knew when a treat was coming so did it with the anticiaption of getting one. I then started just giving praise with no treats. he now will sit, stay and lie down mostly on command without a treat on offer but i still have to get him to do this when outside with distractions more.
Eventually it will become second nature (

) and training treats wont be needed.
saying all that he still has deaf days and i never try to get him to do anything at the moment when he is distracted as here is no point. Although today he was chasing around with 2 dogs on a walk and i whistled him back and he came, followed by the 2 dogs!!
Patience, as they say, is a virtue!!!!!!!!!!!