I want to know how TIM2 of STM32F100 works. Can someone help me?

TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure;

uint16_t PrescalerValue = 0;

RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM2, ENABLE);

TIM_TimeBaseStructure.TIM_ClockDivision = 0;

TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up;

TIM_TimeBaseStructure.TIM_Prescaler = (uint16_t) (SystemCoreClock / 1000) - 1;

TIM_TimeBaseStructure.TIM_Period = 999;

I don't understand why TIM_Period is 999.

The counter goes through 0 to N-1, this has N states. The logic for comparing if the current count is N-1, and resetting/updating at the next clock is simpler to implement.

OutClock = TIMCLK / (P * Q); where Prescaler = Q-1, and Period = P-1

Your example looks to generate a 1Hz update. It prescales the TIMCLK to 1 KHz, and then counts off 1000 ticks.