Why does the solution become yellowish?
Initial lactose aqueous solution contains participants for a potential chemical reaction. On the one hand, it is lactose itself, which is glad to provide electrons. On the other hand, it is oxygen dissolved in water that is willing to accept these electrons. Interestingly enough, though, oxygen isn't that ready to interact with lactose. And methylene blue can help the situation here: this colored compound acts as a carrier in our experiment. It drags electrons from lactose and passes them to oxygen. However, at a certain point there turns to be no more oxygen in the solution. And then, methylene blue that has taken electrons from lactose has nowhere to give them to. And at such state, methylene blue becomes colorless, as we can see.
Why does the solution turn blue again?
We can saturate the solution again with oxygen from the air above the solution. After the flask has been shaken, oxygen from the air dissolves in the solution. Then, the reaction can proceed again, until all the oxygen available in the solution is spent. However, this trick cannot be repeated endlessly. Since the flask is tightly sealed, sooner or later all the oxygen from the air will be depleted, and the solution will then remain yellowish. Nevertheless, the process can be reactivated by opening the flask to let extra air in.
Why did we add alkali to the lactose aqueous solution?
By adding sodium hydroxide NaOH aqueous solution, we created an alkaline environment. This special requirement must be met for methylene blue to accept electrons from lactose. Otherwise, the reaction would not proceed, and the solution would remain blue. You may check it by running the experiment without NaOH.
Why is it so important to seal the flask tightly?
First of all, it is for convenience. This way you may shake the flask without fearing its contents would spill out.
Moreover, by sealing the flask we prevent the ambient air from getting into the flask. Therefore, oxygen from the ambient air will not have access to our solution as well. Therefore, color recovery is only possible after shaking the flask.
The most diligent experimentalists have probably noticed that after the first shake blue coloring doesn't disappear completely. It remains at the border between the solution and air in the flask (along so-called meniscus) and forms a nice blue fringe. The same would happen if the flask is left opened. This is caused by oxygen present in high concentration in the air above the solution. Oxygen penetrates through the liquid-gas interface and transforms methylene blue into its colored form. However, when oxygen is being gradually depleted in the flask, this border gets thinner and finally disappears.