Some people put off telling as long as possible for fear of becoming marginalized (or worse) during the final weeks or months of employment. Others announce their intentions far in advance. Experts often recommend treating retirement procedures and announcements much the same way you would leaving for another job. If your departure will disrupt the organization and significantly affect co-workers or students, then some time will be required to square things away before your last day. I know colleagues who retired but then returned as a part-time, paid employee to finish out projects. I think it's a good idea to have your department chair or lab director make the announcement, which takes the burden off of you and sounds more official.
Although you could tell people long before your retirement date, there is no real reason to and may lead to unexpected and unpleasant consequences. Almost every retiree I've talked to has said that as soon as you make the announcement, you will be treated differently at work. You become a lame duck, and people may begin to write you off sooner than you like. Or they may decide to take advantage of the situation. One colleague said, "As soon as the word gets out, the vultures will start circling." He was referring to colleagues who coveted his lab/office space, equipment, and funding that (they assumed) would be up for grabs when he departed. Not everyone has this experience, but some scientists may find it difficult to give up the resources that they've worked so hard to acquire. Having co-workers prematurely claiming your stuff just makes it worse. I know of at least one scientist who rescinded plans to retire when a young colleague became a bit too aggressive in taking over his program.
If your students and/or projects will continue beyond your retirement and are dependent upon those resources, then it's important to work out a transition plan with your superiors. By working out a plan to transfer space and resources to someone of your choice (and getting official approval for that transfer) before announcing your retirement will help discourage the vultures and ensure your projects and students are taken care of in your absence. Once you retire, you will have no say in such matters.
I decided a year ahead as to the approximate date of retirement and contacted the person responsible for guiding employees through the process (usually such people must keep your information confidential). I selected the precise date about two months before termination (this was my agency's recommended time frame). Then I decided on what date to inform my supervisor and scheduled a meeting with him. It's a good idea to have all your paperwork in order with HR before you meet with your boss. Depending on your relationship, the meeting may be a brief, formal interaction or a more friendly conversation. If appropriate, you can discuss the possibility of becoming an emeritus scientist/professor and negotiate what that status might entail. At such a meeting you also can discuss having your boss make the announcement—and when—so that you have time to tell close co-workers and your staff and students personally (you don't want them to find out in an email announcement). I set up a meeting with staff to tell them right after I met with my supervisor. I met individually with close co-workers/friends to tell them.
When you announce your retirement (and how) is obviously an individual choice and dependent on a number of factors. However, it's important to give it some thought ahead of time.