In 2011, I visited a LDS church. On my visit, I noticed that bread and water were served. At the time, I merely thought they were some sort of snack for the congregation, and I gladly took a tiny cup of water and a piece of bread. The people didn't seem to care about my taking communion. Does that mean it is okay?
The breaking, blessing, and partaking of the bread and water constitutes the sacrament ordinance in the LDS Church. It's how one renews their baptismal covenants. If one has not made those covenants, it is of no matter. (Parents often encourage their children to take the sacrament to prepare them to one day renew their covenants and to teach them the value of the ordinance. Others discourage it because it has special meaning.)
Either way, the sacrament is not withheld from individuals while being passed, whether they are visitors or not. It is perfectly acceptable for visitors in an LDS congregation to partake of the sacrament:
Although the sacrament is for Church members, the bishopric should not announce that it will be passed to members only, and nothing should be done to prevent nonmembers from partaking of it.
Matt has described the current policy of the LDS church accurately - namely, that nobody in attendance should be discouraged from taking of the sacrament. For informational purposes, however, it is useful to know that his policy was enacted following years of fairly inconsistent practices from congregation to congregation. As the LDS Church has grown globally, its leaders have clarified certain policies to accomplish international consistency.
- My father reports that in the 1970s when my grandfather was a local leader in Southern California, the regional president instructed him that only children and baptized members were permitted to take the sacrament.
- While serving an LDS mission, we occasionally had to take local leaders aside and let them know about the policy mentioned above (Handbook 2: Administering the Church, 20.4.1). These leaders were mostly older Europeans who spoke little English and were simply propagating what they had seen in their youth.
Bearing both the previous answer and this one in mind give a broad view of the official policy, while explaining why individual discrepancies arise.