If you come across your puppy in the act of soiling the carpet or floor, you should sternly say, "No" but without yelling. But if the puppy has already made the mess when you find it don't say anything as it will not understand what you are correcting it for. This is a very important point as puppies and learn by association. Rubbing your puppy's face in its excrement or spanking it will not have any positive effects towards training it, so just don't do it.
Some puppies learn faster while others take more time and for this reason it is important that you the puppy owner does not become frustrated or angry if the puppy does not learn something within the time frame that you have decided on. While out walking do not let your puppy eliminate on the sidewalk, as this is where people walk. Accidental urination caused by excitement or fear is a normal reaction in many puppies and they should never be punished or scolded for this. They will normally grow out of this behavior, as they get older.